2017-02-27 12 views
0

Do While Loop和ExpectedConditions - 可以一起使用嗎?Do While Loop和ExpectedConditions - 可以一起使用嗎?

  1. Iam奇怪無法找到元素/超時異常與下面列出的方法。

2.是否可以添加一個while循環,以便該方法可以檢查並執行多次?

public void waitAndClickElement(WebElement element) throws InterruptedException { 
    try { 
     this.wait.until(ExpectedConditions.elementToBeClickable(element)).click(); 
     System.out.println("Successfully clicked on the WebElement: " + "<" + element.toString() + ">"); 
    }catch (Exception e) { 
     System.out.println("Unable to wait and click on WebElement, Exception: " + e.getMessage()); 
     Assert.assertFalse(true, "Unable to wait and click on the WebElement, using locator: " + "<" + element.toString() + ">"); 
    } 
} 

回答

1

是的,這是可能的。事情是這樣的:

boolean clicked = false; 
    int attempts = 0; 
    while(!clicked && attempts < 5) { 
    try { 
      this.wait.until(ExpectedConditions.elementToBeClickable(element)).click(); 
      System.out.println("Successfully clicked on the WebElement: " + "<" + element.toString() + ">"); 
      clicked = true; 
    } catch (Exception e) { 
      System.out.println("Unable to wait and click on WebElement, Exception: " + e.getMessage()); 
      Assert.assertFalse(true, "Unable to wait and click on the WebElement, using locator: " + "<" + element.toString() + ">"); 
     } 
     attempts++; 
    } 

這可能是一個好主意,一個最大的嘗試添加到您的while循環,所以你不要讓她陷入了一個無限循環。

+0

我會添加另一個內部的最大嘗試嗎?感謝你的幫助 – Gbru

+0

不,你可以把它合併到現有的。虐待編輯 – Cathal

+0

再次非常感謝@Cathal – Gbru