6

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.

4

6 に答える 6

4
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.

于 2012-08-29T12:45:59.440 に答える
3

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'

于 2012-08-29T14:48:27.290 に答える
1

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();
于 2012-08-29T12:26:34.750 に答える
1
    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
于 2018-06-01T12:48:48.230 に答える
0

I have Add Attachment button:

I tried by this code:

driver.findElement(By.xpath("//*[@id=\"attachments\"]/div/div/img")).sendKeys("C:\\Users\\NayazPasha\\Desktop\\Ndin selenium Testing outputs\\Collab Schedule onclick.png");
于 2020-09-15T11:24:05.510 に答える
-1

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?

于 2012-08-29T11:58:21.023 に答える