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

What is the best way to highlight a specific city on a map by placing a circle around it?

I am currently developing a project that involves creating a circle around the location of an item on a Google Map. In the code snippet below, when the show tab is clicked, it should display a circle around the item's location on the map: <div cla ...

Unpredictable Dependency Outcomes in Node.js

Encountering a warning while running yarn install where all dependencies are installed, but a specific warning is triggered: Warning: Pattern ["esprima-fb@~3001.0001.0000-dev-harmony-fb"] is attempting to unpack in the same destination "/Users/Me/Librar ...

Typescript check for type with Jest

Assume there is an interface defined as follows: export interface CMSData { id: number; url: string; htmlTag: string; importJSComponent: string; componentData: ComponentAttribute[]; } There is a method that returns an array of this obj ...

Aggregating documents in MongoDB based on specific criteria shared by all members of the group

I'm currently working with a set of example documents structured like this: /* 1 */ { "_id" : ObjectId("61c50482f176d72cb660baa3"), "answer" : "yes",... /* 2 */ { "_id" : ObjectId(&quo ...

Error encountered in the main thread: Could not find class definition for org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec

An issue occurred in the main thread with the message: java.lang.NoClassDefFoundError: org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec Could this error be related to dependencies? I tried searching for solutions on Stack Overflow but couldn' ...

Ways to make JavaScript cycle through a set of images

I'm having trouble trying to set up a code that will rotate multiple images in a cycle for an image gallery I'm working on. So far, I've only been able to get one image to cycle through successfully. Any help or suggestions would be greatly ...

Vue is refusing to display information for a certain API call

Within my next component, I have the following structure: <template> <div class="home"> <div class="container" v-if="data" > <Card v-for="result in data" :img="result.links[0]&q ...

Discovering the URL of the Submit Button using Selenium in Python

I'm attempting to access the URL linked to the submit button: https://i.sstatic.net/ZpLwS.png from selenium import webdriver browser = webdriver.Safari(executable_path = '/usr/bin/safaridriver') #Enter the required information default_i ...

Where can I locate the org.testng.TestNG package within the TestNG framework while using the Eclipse IDE for Java development?

Currently, I am in the process of creating a unique automation framework for testing purposes. My design involves using Selenium Webdriver along with Java as the programming language. In order to generate reports effectively, I am looking to incorporate t ...

I am currently exploring next.js and working on creating a dedicated single post page within my project

I am currently working with Next.js and fetching some dummy data on the homepage. However, I am facing an issue when trying to create a separate page for each post obtained from the homepage. Although I have already coded it, I feel like there is room fo ...

Ways to extract a Bearer Token from an Authorization Header using JavaScript (Angular 2/4)

When working with JavaScript, I have successfully implemented a method for authenticating to my server using an http post request. Upon receiving a response from the server, it includes a JWT in an Authorization header like this: Authorization: Bearer my ...

The parameter always comes up empty when using $state.go in AngularJS

When trying to pass one parameter using `$stete.go`, I encountered an issue. ROUTER $stateProvider.state('login.signin', { url: '/signin', params: { login_event: '' }, templateUrl: "assets/views/login ...

Searching for a way to detect when a user clicks the back button on their Android or iPhone device using JavaScript or

In the process of building a Single Page Application (HTML5) utilizing the following plugins: - Sammy.js - Knockout.js - Require.js - Jquery.js This app is designed for Android, iPhone, and Windows mobile devices. Many scenarios revolve around cli ...

react-router is unable to navigate to the desired page

I am currently using react-router-dom in my project, but I am still relatively new to it. I have encountered an issue regarding page navigation. Below is the code for my App.js: class App extends Component { render() { return ( <div classN ...

Modify the background color of checkboxes without including any text labels

I am looking to customize my checkbox. The common method I usually see for customization is as follows: input[type=checkbox] { display: none; } .my_label { display: inline-block; cursor: pointer; font-size: 13px; margin-right: 15px; ...

Combining the power of Selenium with React using Python

My current project involves utilizing Selenium WebDriver with Python to automate the testing of some React code as part of my BDD learning journey. I've encountered a peculiar issue while trying to conduct a specific test. I developed a simple app co ...

Is there a guarantee that XHR requests made within a click handler of an anchor tag will always be sent?

I'm trying to find information on whether an XHR request triggered in an anchor tag's click event handler is guaranteed to be sent. Despite my attempts at searching, I can't seem to locate a definitive answer. The specific question I have i ...

Tips for finding the element

This particular element functions as a button and will only appear once an option is chosen from the drop-down menu. I am encountering difficulty in identifying its location using xpath <td align="center" id="patientstatus" style="width:84px;"> ...

Navigating through Objects in Angular 9

I am facing a challenge in Angular 9/Typescript while trying to iterate through the object response from my JSON data. Despite searching for solutions, I haven't found any that work for me. In my JSON, there is a section called "details" which contain ...

Is it possible to simultaneously run two Node.js projects on Windows?

Is it possible to run two Node.js projects on a Windows operating system? If so, what is the process for doing that? If not, can I run two Node.js projects on a dedicated host instead? ...