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.

6 Answers 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.

Sign up to request clarification or add additional context in comments.

Comments

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'

Comments

2

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();

Comments

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

Comments

0

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?

1 Comment

Or that one yes :) As you can see, lots and lots of way to find elements.
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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.