Need help figuring out how to use Javascript:void(0) to click a button in Selenium?

Struggling with finding the right solution, I have attempted various methods. Apologies for any formatting errors, but the element that requires clicking is:

<button href="javascript:void(0)" id="payNewBeneficiary" class="button-new-payee">
                                            <div class="icon">
                                                <div></div>
                                            </div>
                                            <h2>Someone new</h2>
                                            <p>Make a once-off payment or pay someone new</p>
                                        </button>

I have experimented with locating the element by ID, cssSelector, and xpath. Additionally, I have tried the following approaches:

Actions act = new Actions(driver); 
act.moveToElement(payNewBeneficiaryButton);
act.click(payNewBeneficiaryButton);
        act.build().perform();

As well as:

((JavascriptExecutor)driver).executeScript("document.getElementById('payNewBeneficiary').click()");

And also:

JavascriptExecutor exec = (JavascriptExecutor) driver;
exec.executeScript("arguments[0].click()", payNewBeneficiaryButton);

Answer №1

Give these a shot

driver.findElement(By.css("<one of the options below>")).click()

Possible css selectors to attempt:

button#payNewBeneficiary div div
button#payNewBeneficiary div
button#payNewBeneficiary h2
button#payNewBeneficiary p

Occasionally, the event listener might be on a specific child element

Answer №2

After encountering an issue where the element was not clickable, I resolved it by implementing a script to scroll down the page until the element was no longer obstructed.

 wait.until(ExpectedConditions.elementToBeClickable(payNewBeneficiaryButton));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();",payNewBeneficiaryButton);
payNewBeneficiaryButton.click();

Thank you for your assistance in trying to solve the problem. Your efforts are greatly appreciated.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Strange occurrences within the realm of javascript animations

The slider works smoothly up until slide 20 and then it suddenly starts cycling through all the slides again before landing on the correct one. Any insights into why this is happening would be greatly appreciated. This issue is occurring with the wp-coda- ...

Is it possible to perform direct URL searches using react-router-dom?

Encountering an issue when attempting to directly copy a route, resulting in the following error: Error: Cannot Access / home Despite utilizing various methods such as browserHistory, I am unable to successfully render views when navigating with menu i ...

What is the best way to implement a scrollbar in a specific div rather than the entire window?

Hey everyone! So, I have this window in electronJS with a div where I'm dynamically adding elements using Javascript and the function --> document.createElement('section'). Here's the loop in Javascript to add these elements: for (var ...

Display the names of files depending on the selection made in the drop-down menu

In order to utilize a value from a select box for a PHP function later on the page, I am seeking assistance. Within my index.php file, there is a standard select drop down containing folder names as values and options. After selecting a folder, I aim to e ...

Utilize date-fns to style your dates

Struggling to properly format a date using the date-fns library. The example date I'm trying to work with is 2021-09-20T12:12:36.166584+02:00. What am I doing wrong and what is the correct approach? Here's the code snippet I have so far: import ...

What could be causing Jest to prevent me from using the node testing environment, especially when they have made changes to it?

I am currently working on testing a React component within Next.js using Jest. Jest made an official announcement regarding the change of default test environment to Node: Due to this update, the default test environment has been switched from "jsd ...

Exploring Angular 2 Routing across multiple components

I am facing a situation where I have an app component with defined routes and a <router-outlet></router-outlet> set. Additionally, I also have a menu component where I want to set the [routerLink] using the same routes as the app component. How ...

The Concept of Interface Segregation Principle within jQuery

Could someone provide a clear explanation of how this function operates using jQuery? Especially in reference to the response found here. It seems similar to the Single Responsibility Principle (SRP) in Object-Oriented Programming. What sets it apart? ...

Accessing XML files locally via JavaScript on Chrome or Internet Explorer, with compatiblity for Android mobile devices as well

Looking to extract and display data from an XML file directly in an HTML page without the need for a web server. Ready to dive into using JavaScript, jQuery, or Ajax to get the job done. ...

Switching a jQuery AJAX Response

Currently, I am utilizing an AJAX function to retrieve and display specific categorical posts when a corresponding button is clicked: <script> // Brochure AJAX function term_ajax_get(termID) { jQuery("#loading-animation").show(); ...

The functionality of ng-table appears to be compromised when working with JSON data

Currently, I am facing an issue while using a JSON file to populate data in my Angular ng-table. Even though the JSON data is being displayed in a regular table format, certain functionalities of ng-table like pagination and view limit are not functioning ...

Scrolling through a list while the user types in the search bar

I am currently working on a table that populates based on an array of country objects, and I have added a search bar to enable live filtering through the countries array. However, as a newcomer to Vue, I am struggling to implement this feature effectively. ...

Storing Extracted Information into CSV using BeautifulSoup

When iterating through rows in a table within the page_soup, the script retrieves content from specific columns and prints them out. The first_column is selected from the first 'td' element, second_column from the second 'td', and third ...

Error: USB connection issue detected at line 415 in usb_service_win.cc. Unable to retrieve device interface GUIDs due to file not found (0x2)

Snippet of my Python Code using Selenium: from selenium import webdriver import time web = webdriver.Chrome() web.get('https://fs30.formsite.com/LB2014/wa03ei3cqi/index.html') time.sleep(2) FirstName = "Test" first ...

Preventing the use of double-click on Grooveshark

I am currently in the process of creating a webpage to serve as a jukebox for parties. My goal is to have the page load Grooveshark with a transparent overlay (or any other necessary method) that can prevent users from forcefully adding their song to the f ...

extracting data from a website using selenium and storing it in an array

I need assistance with extracting text content from a website and organizing it into an array. Specifically, I want to extract plaintext by identifying the corresponding html element. My current approach involves using selenium with Java, but I am seekin ...

Storing a single JavaScript array in separate arrays depending on a specific condition

I have a JavaScript array variable where each element is an object with values like: {"ID":10075,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":11,"GROUPID":1} The entire JSON.stringify value looks like this: "[{"ID":10075,"SPECIALIZATIONID":"17","PRESP ...

Tips for expanding com.googlecode.wicket.jquery.ui.widget.tabs.TabbedPanel in Wicket

I am looking to customize a dynamic tabbed panel in Apache Wicket 7.3 by adding and removing tabs using AjaxTabs. Since the AjaxTab is based on JQuery, I plan to utilize TabbedPanel from this extension for my project. To enable removal of tabs, I aim to i ...

how to adjust the width of table columns dynamically using ng-repeat and AngularJS

Working on a user interface that contains a table where the column widths adjust according to percentage data. I'm using ng-repeat to populate the table, but need help setting unique widths for each piece of data received. Here's some of my Angul ...

Change not accepted

I am a beginner in Angular and still grappling with the fundamentals. On my menu, I have a cart icon with an initial value of 0 upon first load. In my product list, each product has an 'AddToCart' button. What I aim to achieve is- I want to dy ...