Selenium 中有哪些重要的異常?
Selenium 中各種重要的異常列舉如下:
TimeoutException − 如果操作在特定時間內未完成,則丟擲此異常。如果頁面元素即使在等待後也沒有載入。
driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS) ; driver.get(“https://tutorialspoint.tw/index.htm” );
在上面的程式中,添加了 15 秒的隱式等待。如果頁面https://tutorialspoint.tw/index.htm在 15 秒內未載入,則會丟擲 TimeoutException。
NoSuchElementException − 如果頁面上不存在具有特定屬性的 Web 元素,則會發生此異常。此異常類是 NotFoundException 的子類,如果驅動程式未能成功定位元素,則會丟擲此異常。
driver.findElement(By.name("tutorial-test")).click(); //Exception Handling: try { driver.findElement(By.name("tutorial-test")).click(); } catch (NoSuchElementException e)
ElementNotVisibleException − 如果 Web 元素不可見但在 DOM 中存在,則會發生此異常。它是 ElementNotInteractable 類的子類。在驅動程式嘗試對不可見或隱藏的元素執行操作的場景中,會丟擲此異常。在此,如果具有屬性名稱 tutorial-test 的元素被隱藏,則會發生此異常。
driver.findElement(By.name("tutorial-test")).click(); //Exception Handling: try { driver.findElement(By.name("tutorial-test")).click(); } catch (ElementNotVisibleException e)
StaleElementException - 如果 Web 元素不存在,因為它已被刪除或不再附加到 DOM,則會發生此異常。此異常與 ElementNotVisibleException 有細微的差別。
在一些情況下,頁面上成功建立了一個特定的 Web 元素,但是目前它可能由於 DOM 重新整理、添加了新的導航頁面或切換到框架或視窗而缺失。
廣告