问题
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.
回答1:
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'
回答2:
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.
回答3:
The followings should work:
driver.findElement(By.className("buttonLargeAlt")).click();
driver.findElement(By.xpath("//button[contains(@class='buttonLargeAlt')]")).click();
driver.findElement(By.xpath("//button[@class='buttonLargeAlt']")).click();
回答4:
You can achieve this by using XPath with html input element id or by name
//1. By XPath indexing option:
WebElement loginButtonId =
driver.findElement(By.xpath("//*[@id='login']"));
//Xpath of login button i have get For firefox browser
loginButtonId.click();
I hope this work for you
回答5:
That XPath will only get the span, which will not be the physical button.
Works perfectly fine here:
//span[contains(.,'Checkout')]/button
or By.CssSelector:
button.buttonLargeAlt
If still not working, explain more. Is it in an iFrame? What error does Selenium give?
来源:https://stackoverflow.com/questions/12176170/how-can-i-click-on-a-button-using-selenium-webdriver-with-java