I have a list of <li> tags and I need to click on the one that contains the certain number (let's say 4).
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
and my code is:
int a = 4;
List<WebElement> allElements = driver.findElements(By.xpath("//div[@class='divClass']/ul[@class='ulClass']/li"));
for (WebElement element: allElements) {
String bText = element.getText();
int b = Integer.parseInt(bText);
System.out.println(a + " ? " + b);
if (a == b){
element.click();
break;
}
}
The Eclipse result is:
1 ? 4
2 ? 4
3 ? 4
4 ? 4
5 ? 4
I don't get it what's wrong with the if statement (as the element.click(); or break; not working)... can anyone help please?
I think you can change your xpath expression by
"//div[@class='divClass']/ul[@class='ulClass']/li[text()='4']"
To get the li tag with the value you want
Try below code:-
String value = driver.findElement(By.xpath("//div[@class='divClass']/ul[@class='ulClass']/li[4]")).getText();
Hope it will help you :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With