I am trying to automate clicking of a button, and I am using the following code to do that :
driver.findElement(By.xpath("//*[contains(text(),'" deno "')]")).click();
where deno
is simply a variable.
When deno
= 14 Diamonds, the click action can be automated. The following is the screenshot of the 14 Diamonds' location.
However, when deno
= 42 Diamonds, the button was not clicked and no error (such as element not found) was displayed. The following is the screenshot of the 42 Diamonds' location.
I am very new to this, so hope to have some advice on what I may have done wrong.
CodePudding user response:
Seems to be new line character in between the text. Can you try below xpath to click on button.
//div[contains(@area-labelledby,'" deno "')]
CodePudding user response:
Rather than searching for the elements via text, you can use the class names and create a css selector, get all the web elements which pertain to the text buttons and then perform what ever actions you want to perform.
If you see the HTML, that you've provided, then you can see that the buttons have common classes. - selection_tile__text d_flex flex-column justify-content-center text-center
[Please correct if there is any spelling mistake since I manually typed all these classes]
Now you can get all the buttons using the classes from above -
List<WebElement> allBtns = driver.findElements(By.cssSelector("selection_tile__text.d_flex.flex-column.justify-content-center.text-center"));
for (WebElement btn : allBtns){
btn.click(). // will click on every button
// do something else
}