Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement (element.click or break) not working

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?

like image 602
Darkkrad Avatar asked May 04 '26 20:05

Darkkrad


2 Answers

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

like image 144
Maxence Cramet Avatar answered May 06 '26 10:05

Maxence Cramet


Try below code:-

String value = driver.findElement(By.xpath("//div[@class='divClass']/ul[@class='ulClass']/li[4]")).getText();

Hope it will help you :)

like image 21
Shubham Jain Avatar answered May 06 '26 08:05

Shubham Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!