Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click Onclick Javascript form using Selenium?

This is my source code :

<div class="emph a-center addCard"
Or 
<a class="add-link" href="javascript:;" onclick="gotoPaymentAddressForm()">Add a New Credit Card</a
</div>

I need to click "Add New Credit" card which open form on the same page. Unable to do this using Selenium.

Kindly advice.

like image 565
Vaibhav Jain Avatar asked May 02 '14 10:05

Vaibhav Jain


People also ask

What is click () method in Selenium?

For interacting with web elements in test automation, QA engineers use the Click command offered by Selenium WebDriver. It helps simulate the click action users perform in the real world. However, there are multiple subsets of the click action – left-click, right-click, double click, drag, and drop, etc.

How do I select a form in Selenium?

Select se=new Select(driver. findElement(By.id("nation"))); se. selectByValue("Ind"); There are times while performing Selenium automation testing of our web app, where we need to validate the options coming in our dropdown list, or if we can select multiple options.

How do I click a button using Selenium?

We can click a button with Selenium webdriver in Python using the click method. First, we have to identify the button to be clicked with the help of any locators like id, name, class, xpath, tagname or css. Then we have to apply the click method on it. A button in html code is represented by button tagname.

Can Selenium interact with JavaScript?

Selenium is an open-source automation testing tool that supports a number of scripting languages like C#, Java, Perl, Ruby, JavaScript, etc.

How to use JavaScript in selenium to click an element?

How to use JavaScript in Selenium to click an Element? We can use the JavaScript Executor in Selenium to click an element. Selenium can execute JavaScript commands with the help of the method executeScript.

How to login to a selenium site using click button?

The Selenium click button can be accessed using the click () method. Find the button to Sign in Click on the “Sign-in” Button in the login page of the site to login to the site.

What is selenium form webelement?

Selenium Form WebElement: TextBox, Button, sendkeys (), click () Selenium Form WebElement: TextBox, Button, sendkeys (), click () Forms are the fundamental web elements to receive information from the website visitors. Web forms have different GUI elements like Text boxes, Password fields, Checkboxes, Radio buttons, dropdowns, file inputs, etc.

How to use click button in selenium with lambdatest?

The most basic operation using a Selenium click button method is a left-click or a mouse click. Test Scenario: Visit LambdaTest Homepage and click on the Login button. Example code for the test scenario: You can also use the Selenium .click () button for enabling/disabling checkboxes, radio buttons.


2 Answers

Try with following:

driver.findElement(By.linkText("Add a New Credit Card")).click();
like image 81
Alpha Avatar answered Sep 28 '22 08:09

Alpha


You can also use following xpath codes:

driver.findElement(By.xpath("//a[@onclick='gotoPaymentAddressForm()']")).click();

or

driver.findElement(By.xpath("//a[contains(text(),'Add a New Credit Card')]")).click();
like image 43
Abhishek Yadav Avatar answered Sep 28 '22 07:09

Abhishek Yadav