Selenium WebDriver 中的等待型別

在執行任何 Web 應用程式時,需要考慮載入時間。如果你的程式碼試圖訪問尚未載入的任何元素,WebDriver 將丟擲異常並且你的指令碼將停止。

等待有三種型別 -

  • 隱含等待
  • 明確的等待
  • 流利的等待

隱式等待用於設定整個程式的等待時間,而顯式等待僅用於特定部分。

隱含的等待

隱式等待是告訴 WebDriver 在嘗試查詢一個或多個元素(如果它們不是立即可用)時輪詢 DOM 一段時間。隱式等待基本上是告訴 WebDriver 如果 WebDriver 正在尋找的指定 web 元素不存在你想看到的延遲的方式。預設設定為 0.設定後,將為 WebDriver 物件例項的生命週期設定隱式等待。使用以下程式碼段在程式碼的例項化部分中宣告隱式等待。

Java 中的示例 :

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// You need to import the following class - import java.util.concurrent.TimeUnit;

C#中的示例 :

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));

所以在這種情況下,你告訴 WebDriver,如果在 UI(DOM) 上沒有指定的元素,它應該等待 15 秒。

明確的等待

當某個元素需要更多時間載入時,你可能會遇到例項。設定隱式等待這種情況沒有意義,因為瀏覽器會在每個元素的同一時間不必要地等待,從而增加了自動化時間。顯式等待有助於通過繞過某些特定元素的隱式等待來完成。

顯式等待是限於特定 Web 元素的智慧等待。使用顯式等待你基本上是告訴 WebDriver 它最多是在它放棄之前等待 X 個單位的時間。

顯式等待使用 WebDriverWait 和 ExpectedConditions 類完成。在下面的示例中,對於 id 為 username 的元素,我們將等待最多 10 秒,然後再繼續執行下一個命令。這是步驟。

Java 中的示例 :

//Import these two packages:
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

//Declare a WebDriverWait variable. In this example, we will use myWaitVar as the name of the variable.
WebDriverWait myWaitVar = new WebDriverWait(driver, 30);

//Use myWaitVar with ExpectedConditions on portions where you need the explicit wait to occur. In this case, we will use explicit wait on the username input before we type the text tutorial onto it.
myWaitVar.until(ExpectedConditions.visibilityOfElementLocated(By.id(`username`)));
driver.findElement(By.id(`username`)).sendKeys(`tutorial`);

ExpectedConditions 類具有一些等待元素的預定義公共條件。單擊此處以檢視 Java 繫結中的這些條件的列表。

C#中的示例 :

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.PhantomJS;

// You can use any other WebDriver you want, such as ChromeDriver.
using (var driver = new PhantomJSDriver())
{
    driver.Navigate().GoToUrl("http://somedomain/url_that_delays_loading");

    // We aren't going to use it more than once, so no need to declare this a variable.
    new WebDriverWait(driver, TimeSpan.FromSeconds(10))
        .Until(ExpectedConditions.ElementIsVisible(By.Id("element-id")));

    // After the element is detected by the previous Wait, 
    // it will display the element's text
    Console.WriteLine(driver.FindElement(By.Id("element-id")).Text);
}

在此示例中,系統將等待 10 秒,直到元素可見。如果超時後元素不可見,WebDriver 將丟擲一個 WebDriverTimeoutException

請注意:如果元素在 10 秒超時之前可見,系統將立即繼續進行進一步處理。

流利的等待

與隱式和顯式等待不同,流暢等待使用兩個引數。超時值和輪詢頻率。假設我們將超時值設定為 30 秒,將輪詢頻率設定為 2 秒。WebDriver 將每 2 秒檢查一次元素,直到超時值(30 秒)。超過超時值後沒有任何結果,丟擲異常。下面是一個示例程式碼,顯示了流暢等待的實現。

Java 中的示例 :

Wait wait = new FluentWait(driver).withTimeout(30, SECONDS).pollingEvery(2, SECONDS).ignoring(NoSuchElementException.class);

WebElement testElement = wait.until(new Function() {
    public WebElement apply(WebDriver driver) {
        return driver.findElement(By.id("testId"));
    }
});

使用流暢等待的另一個好處是,我們可以在等待時忽略特定型別的異常(例如 NoSuchElementExceptions)。由於所有這些規定,流暢的等待有助於 AJAX 應用程式以及元素載入時間經常波動的情況。流暢等待的策略性使用顯著改善了自動化工作。