2016-04-24 48 views
0

我試圖把休息一下的元素之後,但點擊它試圖重複再次如何打破,如果循環在Java中

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; 
      } 
     } 
    } 
+0

Ahy幫助表示讚賞! – pals

+0

你想破壞兩個循環嗎? –

+0

我只想點擊元素,然後從整個循環中出來。不應該做任何操作 – pals

回答

0

使用標籤從外環打破元素之後:

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

當你寫break像你有你只是打破了大多數本地環路(在這種情況下是for(WebElement columnElement : columnElements)):如果你的外部環路LIK設置一個循環名

Ë

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

然後你可以打破它,如下面的代碼:

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; 
     } 
    } 
} 

這將帶你出去兩個環,這似乎是你要求什麼的。

+0

感謝它的工作。 – pals

+0

@pals確保接受答案,因爲它可以在未來幫助其他人,謝謝! –