• Selenium Video Tutorials

Selenium WebDriver - 等待支援



Selenium Webdriver 可以與各種等待機制一起使用,例如顯式隱式流暢等待,以實現同步並提供等待支援。等待主要應用於測試中,以處理當 Web 元素在 Selenium 測試預期它在頁面或 DOM 中可用時不可用的情況。

整個頁面載入完畢之前,通常會存在一些延遲時間,並且 Web 元素在網頁上完全可用。Selenium Webdriver 中提供的等待機制有助於在元素以正確的狀態出現在網頁上/消失之前,阻止測試執行。

Selenium Webdriver 中可用的基本等待

Selenium Webdriver 中有多種等待機制可用。它們列在下面:

隱式等待

這是 Selenium 中可用的預設等待。它是一種適用於整個驅動程式會話的全域性等待。預設等待時間為 0,這意味著如果找不到元素,則會立即丟擲錯誤。

顯式等待

它類似於新增到程式碼中的迴圈,該迴圈輪詢網頁以使特定場景在退出迴圈並移動到下一行程式碼之前變為 true。

流暢等待

這是驅動程式等待特定元素條件為 true 的最長時間。它還確定驅動程式在找到元素或丟擲異常之前將進行驗證的間隔(輪詢間隔)。流暢等待是一種自定義顯式等待,它提供選項以自動處理特定異常以及在發生異常時使用自定義訊息。FluentWait 類用於向測試新增流暢等待。

語法

Wait wt = new FluentWait(driver)
   .withTimeout(20, TimeUnit.SECONDS)
   .pollingEvery(5, TimeUnit.SECONDS)
   .ignoring(ElementNotInteractableException.class);
   
wt.until(ExpectedConditions.titleIs("Tutorialspoint"));   

在上面的示例中,指定了超時和輪詢間隔,這意味著驅動程式將等待 20 秒,並在超時時間內以 5 秒的間隔進行輪詢,以滿足Tutorialspoint瀏覽器標題條件。如果在該時間範圍內未滿足該條件,則將丟擲異常,否則將執行下一步。

示例 1 - 顯式等待

讓我們以下面的影像為例,我們首先單擊單擊我按鈕。

Selenium Wait Support 1

單擊“單擊我”後,我們將使用顯式等待,並等待文字您已完成動態單擊出現在網頁上。

Selenium Wait Support 2

程式碼實現

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class ExplicitsWait {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // launching a browser and open a URL
      driver.get("https://tutorialspoint.tw/selenium/practice/buttons.php");

      // identify button then click on it
      WebElement l = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[1]"));
      l.click();

      // Identify text
      WebElement e = driver.findElement(By.xpath("//*[@id='welcomeDiv']"));

      // explicit wait to expected condition for presence of a text
      WebDriverWait wt = new WebDriverWait(driver, Duration.ofSeconds(2));
      wt.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='welcomeDiv']")));
      
      // get text
      System.out.println("Get text after clicking: " + e.getText());

      // Quitting browser
      driver.quit();
   }
}

輸出

Get text after clicking: You have done a dynamic click

Process finished with exit code 0

在上面的示例中,單擊單擊我按鈕後獲得的文字為您已完成動態單擊

示例 2 - 流暢等待

讓我們再舉一個下面的頁面的例子,我們首先點選顏色更改按鈕。

Selenium Wait Support 3

單擊顏色更改後,我們將使用流暢等待,並等待按鈕5 秒後可見出現在網頁上。

Selenium Wait Support 4

程式碼實現

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import java.time.Duration;

public class Fluentwts {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // launching a browser and open a URL
      driver.get("https://tutorialspoint.tw/selenium/practice/dynamic-prop.php");

      // identify button then click
      WebElement l = driver.findElement(By.xpath("//*[@id='colorChange']"));
      l.click();

      // fluent wait of 6 secs till other button appears
      Wait<WebDriver> w = new FluentWait<WebDriver>(driver)
         .withTimeout(Duration.ofSeconds(20))
         .pollingEvery(Duration.ofSeconds(6))
         .ignoring(NoSuchElementException.class);

      WebElement m = w.until(ExpectedConditions.visibilityOfElementLocated
         (By.xpath("//*[@id='visibleAfter']")));

      // checking button presence
      System.out.println("Button appeared: " + m.isDisplayed());

      // Quitting browser
      driver.quit();
   }
}

輸出

Button appeared: true

Process finished with exit code 0

在上面的示例中,我們觀察到單擊顏色更改按鈕後獲得的按鈕為5 秒後可見

結論

本教程全面介紹了 Selenium Webdriver 等待支援。我們首先介紹了 Selenium Webdriver 中可用的基本等待,並透過示例說明了 Selenium Webdriver 中的顯式和流暢等待。這使您能夠深入瞭解 Selenium Webdriver 等待支援。明智的做法是繼續練習您所學的內容,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬視野。

廣告