-3

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;
    }
}
Viraj Shah
  • 754
  • 5
  • 19
  • Two main problems: (1) instead of `false` you should use `return false;`, (2) that `return false;` should be moved *after the loop*. Aside from that `return true` needs `;` after it. – Pshemo May 01 '23 at 11:55
  • Thanks I realized this after coping and pasting code here. Think I did come up with a solution. – MRQAtester May 02 '23 at 19:32

1 Answers1

0

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
    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83