0

I tried putting the break after clicking an element, but after clicking the element it tries to iterate again

for (int i = 1; i < tableSize; i++) {       
        final List<WebElement> columnElements = tableRows.get(i).findElements(By.tagName("td"));
        for(WebElement columnElement : columnElements) {
            if(columnElement.getText().equalsIgnoreCase(alias)) {

                findElement(By.xpath(button.replace("{rowValue}", String.valueOf(i)))).click(); 
                findElement(By.xpath(("//tr[{rowValue}]" + text).replace("{rowValue}", String.valueOf(i)))).click();
                break;
            }
        }
    }
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
pals
  • 5
  • 4

2 Answers2

1

When you write break like you have you are only breaking the most local loop (which in this case is for(WebElement columnElement : columnElements)):

If you set a loop name for the external loop like

 loopName:
 for (int i = 1; i < tableSize; i++) {
 ....

Then you can break it as shown in the code below:

loopName:
for (int i = 1; i < tableSize; i++) {       
    final List<WebElement> columnElements = tableRows.get(i).findElements(By.tagName("td"));
    for(WebElement columnElement : columnElements) {
        if(columnElement.getText().equalsIgnoreCase(alias)) {

            findElement(By.xpath(button.replace("{rowValue}", String.valueOf(i)))).click(); 
            findElement(By.xpath(("//tr[{rowValue}]" + text).replace("{rowValue}", String.valueOf(i)))).click();
            break loopName;
        }
    }
}

This will take you out of both loops, which seems like what you are asking for.

Benjamin Lowry
  • 3,730
  • 1
  • 23
  • 27
0

Use label to break from outer loop:

outerloop:
for (int i = 1; i < tableSize; i++) {  
    ....
    ....
    for (... another loop ...) {
        .....
        .....
        break outerloop:
    }
}
Raman Shrivastava
  • 2,923
  • 15
  • 26