Selenium之顯示等待WebDriverWait


一個顯式等待就是在繼續執行代碼之前編碼等待定義一個特定條件發生。最糟糕的例子是Thread.sleep(),這設置了一個准確的等待時間。WebDriver提供了一些方便的方法幫助您些代碼來等待要求的時間。WebDriverWaitExpectedCondition的結合就是一種實現的方法。

WebDriver driver = new FirefoxDriver();

driver.get("http://somedomain/url_that_delays_loading");

WebElementmyDynamicElement = (new WebDriverWait(driver, 10))

  .until(new ExpectedCondition<WebElement>(){

         @Override

         public WebElement apply(WebDriver d) {

                   returnd.findElement(By.id("myDynamicElement"));

         }});




//
// 通過判斷 title 內容等待搜索頁面加載完畢,Timeout 設置10秒
(new WebDriverWait(driver, 10))
  .until(new ExpectedCondition<Boolean>() {
     @Override

     public Boolean apply(WebDriver d) {

          return d.getTitle().toLowerCase().endsWith("ztree");

    } });


在拋出TimeoutException之前這會等待最多10秒鍾,或者它找到了元素,在0-10秒之間返回。WebDriverWait默認每500毫秒調用ExpectedCondition直到它成功返回。ExpectedCondition類型的成功返回是布爾值true或非null的返回值。

ExpectedConditions

有些自動化web瀏覽器時常用的條件。下面列出的是每個實現。Java恰巧有方便的方法,因此您不需要編寫一個ExpectedCondition類自己或為它們創建自己的實用程序。

l  元素可點擊–元素顯示並且可用。

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

ExpectedConditions類中包含了一組與定義條件,可用於WebDriverWait。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM