使用PHP Webdriver捕獲異常的正確方法是什麼?我有這樣的事情:Selenium PHP Webdriver - 沒有捕捉到異常?
try {
// Throws a NoSuchElementException if id doesn't exist
$driver->findElement(WebDriverBy::id('loggedIn'));
return TRUE;
}
catch (NoSuchElementException $e) {
// id='loggedIn' doesn't exist
return FAlSE;
}
catch (Exception $e)
{
// Unknown exception
return FALSE;
}
然而,當我到達一個頁面,沒有找到與id
我在尋找的元素,我得到以下錯誤:
Uncaught Facebook\WebDriver\Exception\NoSuchElementException: Unable to locate element: #loggedIn
我我不確定爲什麼我的代碼被封裝在try-catch塊中。
這對'findElement'可能的解決方法,但它不回答爲什麼我無法捕捉異常。 – StackOverflowNewbie
好吧,確保你抓住了正確的'NoSuchElementException'。首先驗證是否爲'NoSuchElementException'導入了正確的類? –
並確保異常拋出此代碼或不在'try..catch'塊內的其他代碼? –