Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I click on a button using Selenium WebDriver with Java?

The following is the HTML code for button:

<span>
<button class="buttonLargeAlt" onclick="javascript:submitCheckout(this.form);"type="submit">Checkout</button>
</span>

I tried driver.findElement(By.xpath("//span[contains(.,'Checkout')]")).click();

It is not working...

Any other ideas? There are 2 buttons with same name on the page.

like image 347
Mike Avatar asked Aug 29 '12 10:08

Mike


2 Answers

driver.submit()

should work. If the order of the buttons in your DOM is always the same, this should work too:

driver.findElements(By.className("buttonLargeAlt")).get(0).click();

if it is the first buttonLargeAlt button on your page.

like image 134
Stilltorik Avatar answered Sep 18 '22 01:09

Stilltorik


Try:

//span/button[text()='Checkout' and @class='buttonLargeAlt']

or

//span/button[text()='Checkout'][1]

Also, if you know which of the 2 buttons you need to click, you can try:

//span/button[text()='Checkout'][1]

Where [1] is the first button found with a text of 'Checkout'

like image 24
CosminO Avatar answered Sep 21 '22 01:09

CosminO