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;
            }
        }
    }
4
  • Ahy help is appreciated! Commented Apr 24, 2016 at 10:55
  • Are you trying to break both loops? Commented Apr 24, 2016 at 10:57
  • I just want to click on the element and then come out of the entire loop. shouldnt do any operation Commented Apr 24, 2016 at 10:59
  • "If loop", really? ;) Commented Jul 6, 2016 at 15:35

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@pals make sure to accept an answer as it could help others in the future, thanks!
0

Use label to break from outer loop:

outerloop:
for (int i = 1; i < tableSize; i++) {  
    ....
    ....
    for (... another loop ...) {
        .....
        .....
        break outerloop:
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.