
- 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 Webdriver 執行異常處理。在開發測試時,我們應該確保指令碼即使出現錯誤也能繼續執行。異常類似於在觸發測試執行時遇到的錯誤。
如果由於元素未找到或預期結果與實際結果不匹配或任何其他原因而發生異常,我們的程式碼應生成異常並以邏輯方式結束測試,而不是突然終止指令碼。異常分為兩種型別:
已檢查異常 - 這些異常可以在程式碼編譯之前處理。
未檢查異常 - 這些異常只能在執行時檢測到,並且比已檢查異常更難以處理。
識別和解決未檢查異常的方法稱為異常處理。Selenium 中的一些異常包括:
ElementNotVisibleException - 當元素存在於 DOM 中但不可見時,會引發此異常。因此,無法對其執行任何操作。
ElementNotInteractableException - 當元素存在於 DOM 中時,會引發此異常。但是,當對其執行操作時,另一個元素會受到影響。
ElementClickInterceptedException - 當無法完成元素單擊命令時,會引發此異常。這是因為接收事件的元素隱藏了請求單擊操作的元素。
ElementNotSelectableException - 當嘗試選擇不可選擇的元素時,會引發此異常。
InsecureCertificateException - 當導航導致出現證書警告時,會引發此異常。這導致建立了已過期和不正確的 TLS 證書。
ErrorInResponseException - 此異常是由於伺服器端錯誤而引發的。
ImeActivationFailedException - 此異常是由於 IME 引擎啟用失敗而引發的。
ImeNotAvailableException - 如果 IME 支援不可用,則會引發此異常。
InvalidElementStateException - 如果命令由於元素狀態無效而未完成,則會引發此異常。
InvalidArgumentException - 如果命令引數無效,則會引發此異常。
InvalidCoordinatesException - 如果操作的座標無效,則會引發此異常。
InvalidCookieDomainException - 為在不同域而不是當前 URL 下新增 Cookie 時引發此異常。
InvalidSwitchToTargetException - 當要切換到的目標視窗或框架不存在時,會引發此異常。
InvalidSelectorException - 如果用於標識元素的選擇器無法獲取 WebElement,則會引發此異常。
MoveTargetOutOfBoundsException - 當 ActionsChains move() 方法的目標無效時,會引發此異常。
InvalidSessionIdException - 如果提供的會話 ID 無效或不存在,並且不是活動會話的一部分,則會引發此異常。
NoSuchFrameException - 當要切換到的目標框架不存在時,會引發此異常。
NoAlertPresentException - 當要切換到的目標警報不存在時,會引發此異常。
NoSuchCookieException - 噹噹前活動瀏覽內容的 Cookie 中沒有匹配的 Cookie 時,會引發此異常。
NoSuchAttributeException - 當元素屬性丟失時,會引發此異常。
UnableToSetCookieException - 當驅動程式無法設定 Cookie 時,會引發此異常。
NoSuchWindowException - 當要切換到的目標視窗不存在時,會引發此異常。
TimeoutException - 當命令執行未在時間範圍內完成時,會引發此異常。
StaleElementReferenceException - 當元素引用當前已過期時,會引發此異常。
UnexpectedTagNameException - 當輔助類找不到正確的 Web 元素時,會引發此異常。
UnexpectedAlertPresentException - 當出現意外警報時,會引發此異常。
NoSuchElementException - 此異常是 NotFoundException 類的子類。當無法在 DOM 中識別元素時,通常由 findElement() 方法引發。
SessionNotFoundException - 當驅動程式嘗試在瀏覽器會話終止或關閉時對網頁執行某些操作時,會引發此異常。
WebDriverException - 當驅動程式在驅動程式會話關閉後立即嘗試工作時,會引發此異常。
讓我們討論一些處理異常的方法:
使用 try-catch 塊
try 指示塊的開始,catch 緊跟在 try 塊之後以解決異常。
語法
try { //Perform Action } catch(Exception e1) { //Catch block 1 }
使用 try 和多個 catch 塊
某些程式碼片段可能產生多個異常。每個catch塊用於處理一種型別的異常。
語法
try { //Perform Action } catch(Exception e1) { //Catch block 1 } catch(Exception e2) { //Catch block 2 }
使用throw和throws
throw關鍵字用於生成自定義異常。如果丟擲異常而沒有處理它,則應該在方法簽名中提到throws關鍵字。
語法
public static void errorHandling() throws Exception try { //Perform Action } catch(Exception e) { throw(e); }
使用try-catch-finally塊
try指示塊的開始,catch緊跟在try塊之後用於解決異常。finally程式碼塊無論指令碼是否丟擲異常都會執行。
語法
try { //Perform Action } catch(Exception e1) { //Catch block 1 } finally { //The finally block always executes. }
printStackTrace() − 此方法提供有關異常、堆疊跟蹤以及執行的其他關鍵資訊。
toString() − 此方法描述異常名稱及其用途。
getMessage() − 此方法描述異常。
讓我們來看一個例子,程式中遇到錯誤,由於程式碼中沒有異常處理,執行在中途停止。
示例
ExceptionsEncountered.java類檔案的程式碼實現。
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class ExceptionsEncountered { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // launch an application and open a URL driver.get("https://tutorialspoint.tw/selenium/practice/auto-complete.php"); // identify element with incorrect xpath value WebElement l = driver.findElement(By.xpath("//*[@id='tag']")); // enter text l.sendKeys("Selenium"); // Quitting browser driver.quit(); } }
輸出
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='tag']"} (Session info: chrome=121.0.6167.160) For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception Process finished with exit code 1
在上面的例子中,我們收到了NoSuchElementException異常。由於在上面的實現中沒有應用異常處理,因此在遇到錯誤後執行立即停止。
最後,收到訊息Process finished with exit code 1,表示程式碼執行不成功。
讓我們看看如何使用try-catch和finally塊在上述測試中新增異常處理。
示例
ExceptionHandlingEncountered.java類檔案的程式碼實現。
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class ExceptionHandlingEncountered { public static void main(String[] args) { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // launch an application and open a URL driver.get("https://tutorialspoint.tw/selenium/practice/auto-complete.php"); //try-catch finally block try { // identify element with incorrect xpath value WebElement l = driver.findElement(By.xpath("//*[@id='tag']")); // enter text l.sendKeys("Selenium"); } catch (NoSuchElementException e){ System.out.println("Catch block executed after try block"); } // finally block finally { System.out.println("finally block executed after try - catch block"); } // Quitting browser driver.quit(); } }
輸出
Catch block executed after try block finally block executed after try - catch block Process finished with exit code 0
在本教程中,我們討論瞭如何使用Selenium Webdriver執行異常處理。