
- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境設定
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 功能
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- Selenium - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼生成
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 簡介
- Selenium WebDriver vs RC
- Selenium - 安裝
- Selenium - 第一個測試指令碼
- Selenium - 驅動程式會話
- Selenium - 瀏覽器選項
- Selenium - Chrome 選項
- Selenium - Edge 選項
- Selenium - Firefox 選項
- Selenium - Safari 選項
- Selenium - 雙擊
- Selenium - 右鍵單擊
- Python 中的 HTML 報告
- 處理編輯框
- Selenium - 單個元素
- Selenium - 多個元素
- Selenium Web 元素
- Selenium - 檔案上傳
- Selenium - 定位器策略
- Selenium - 相對定位器
- Selenium - 查詢器
- Selenium - 查詢所有連結
- Selenium - 使用者互動
- Selenium - WebElement 命令
- Selenium - 瀏覽器互動
- Selenium - 瀏覽器命令
- Selenium - 瀏覽器導航
- Selenium - 警報和彈出視窗
- Selenium - 處理表單
- Selenium - 視窗和選項卡
- Selenium - 處理連結
- Selenium - 輸入框
- Selenium - 單選按鈕
- Selenium - 複選框
- Selenium - 下拉框
- Selenium - 處理 IFrame
- Selenium - 處理 Cookie
- Selenium - 日期時間選擇器
- Selenium - 動態 Web 表格
- Selenium - Actions 類
- Selenium - Action 類
- Selenium - 鍵盤事件
- Selenium - 鍵上/下
- Selenium - 複製和貼上
- Selenium - 處理特殊鍵
- Selenium - 滑鼠事件
- Selenium - 拖放
- Selenium - 筆事件
- Selenium - 滾動操作
- Selenium - 等待策略
- Selenium - 顯式/隱式等待
- Selenium - 支援功能
- Selenium - 多選
- Selenium - 等待支援
- Selenium - 選擇支援
- Selenium - 顏色支援
- Selenium - ThreadGuard
- Selenium - 錯誤和日誌記錄
- Selenium - 異常處理
- Selenium - 其他
- Selenium - 處理 Ajax 呼叫
- Selenium - JSON 資料檔案
- Selenium - CSV 資料檔案
- Selenium - Excel 資料檔案
- Selenium - 跨瀏覽器測試
- Selenium - 多瀏覽器測試
- Selenium - 多視窗測試
- Selenium - JavaScript 執行器
- Selenium - 無頭執行
- Selenium - 捕獲螢幕截圖
- Selenium - 捕獲影片
- Selenium - 頁面物件模型
- Selenium - 頁面工廠
- Selenium - 記錄和回放
- Selenium - 框架
- Selenium - 瀏覽上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架構
- Selenium - 元件
- Selenium - 配置
- Selenium - 建立測試指令碼
- Selenium - 測試執行
- Selenium - 端點
- Selenium - 自定義節點
- Selenium 報告工具
- Selenium - 報告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & 其他技術
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven & Jenkins
- Selenium - 資料庫測試
- Selenium - LogExpert 日誌記錄
- Selenium - Log4j 日誌記錄
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 測試
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium 其他概念
- Selenium - IE 驅動程式
- Selenium - 自動化框架
- Selenium - 關鍵字驅動框架
- Selenium - 資料驅動框架
- Selenium - 混合驅動框架
- Selenium - SSL 證書錯誤
- Selenium - 替代方案
- Selenium 有用資源
- Selenium - 問答
- Selenium - 快速指南
- Selenium - 有用資源
- Selenium - 自動化實踐
- Selenium - 討論
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 - 顯式等待
讓我們以下面的影像為例,我們首先單擊單擊我按鈕。

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

程式碼實現
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 - 流暢等待
讓我們再舉一個下面的頁面的例子,我們首先點選顏色更改按鈕。

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

程式碼實現
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 相關的其他內容,以加深您的理解並拓寬視野。