How to loop through a list of elements and verify if one ends with a specific letter in Java

49 views Asked by At

How would I go about looping through a list of elements and finding out whether the list ends with a specific letter?

My code keeps breaking on the if line saying stale element. Any suggestions?

My code is as follows:

boolean ListEndsWithLetter(String column, String text) {
    List<WebElement> liElements = driver.findElements(By.xpath("//div[@col-id='" + column + "']"));
    for (int i = 0; i < liElements.size(); i++) {
        if (liElements.get(i).getText().endsWith(text))
            return true;
        return false;
    }
}
1

There are 1 answers

0
MRQAtester On

This is what I ended up doing and it seems to be working:

boolean ListEndsWithLetter (String column, String text) {
    List<WebElement> liElements = driver.findElements(By.xpath("//div[@col-id='" + column + "']"))
    for (int i = 0; i < liElements.size(); i++)
    {
        String j = liElements.get(i).getText()
        if (j.endsWith(text)) {
            return true
        }
        false
    }
}