How to click on an element inside span tag? - Selenium

2.6k views Asked by At

Hi I am trying to click on a specific button with text "€11" on it in selenium webdriver in Java.

The button is located within this format and there is also another button on the page with exactly the same code but different price:

<button type="button" class="bui-button bui-spacer--medium bui-button--primary bui-button--wide">
<span class="bui-button__text">€11</span>
</button>

How would I do this. Any help is appreciated :)

Full CSS

2

There are 2 answers

0
raja On

A few options you could use:

driver.findElement(By.xpath("//span[contains(text(),'€11')]")).click();

Click first button using this:

driver.findElement(By.xpath("(//span[@class='bui-button__text'])[1]")).click();

Click second button using this:

driver.findElement(By.xpath("(//span[@class='bui-button__text'])[2]")).click();

Note: use wait's if you have any delays in loading amount

0
Ramon Freitas On

I know some ways that could help

javascript:

document.querySelector("span.bui-button__text").click()

jquery:

$("body > button > span").click()

Selenium (Java):

driver.findelement(By.cssSelector("button[class='bui-button bui-spacer--medium bui-button--primary bui-button--wide']").click()

I hope that they help you !