問題描述
以下代碼偶爾會拋出 org.openqa.selenium.WebDriverException
.
The following code throws occasionally an org.openqa.selenium.WebDriverException
.
WebElement element = driver.findElement(by);
element.click();
(new WebDriverWait(driver, 4, 100)).until(ExpectedConditions.stalenessOf(element));
頁面如下所示(by 是 <a></a>
的選擇器)
The page looks like this (by is a selector for <a></a>
)
<iframe name="name">
<html id="frame">
<head>
...
</head>
<body class="frameA">
<table class="table">
<tbody>
<tr>
<td id="83">
<a></a>
</td>
</tr>
</tbody>
</table>
</body>
</html>
</iframe>
消息是 unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot find context with specified id"}
.element
是 iframe
的一部分,單擊會導致 iframe
的內容重新加載.等待時拋出異常.這個異常是什么意思,我該如何解決?
The message is unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot find context with specified id"}
. element
is part of an iframe
and the click can cause the content of the iframe
to reload. The exception is thrown while waiting. What does this exception mean and how could I fix it?
推薦答案
這個錯誤信息...
unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot find context with specified id"}
...暗示 WebDriver 實例無法找到所需的元素.
...implies that the WebDriver instance was unable to locate the desired element.
正如您在問題中提到的那樣,該元素是 <iframe>
的一部分,并且調用 click()
可能會導致 iframe 的內容在其中重新加載如果您需要遍歷回 defaultContent
并再次使用 WebDriverWait 再次切換回所需的 iframe
然后誘導 WebDriverWait 對于 stalenessOf()
上一個元素或下一個 desired 元素 的存在,如下所示:
As you mentioned in your question that the element is part of an <iframe>
and invoking click()
can cause the content of the iframe to reload in that case you need to traverse back to the defaultContent
and again switch back again to the desired iframe
with WebDriverWait and then induce WebDriverWait either for stalenessOf()
previous element or presence of next desired element as follows :
WebElement element = driver.findElement(by);
element.click();
driver.switchTo().defaultContent(); // or driver.switchTo().parentFrame();
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("xyz")));
// wait for stalenessOf previous element (visibility of next desired element preferred)
new WebDriverWait(driver, 4, 100).until(ExpectedConditions.stalenessOf(element));
// or wait for visibility of next desired element (preferred approach)
new WebDriverWait(driver, 4, 100).until(ExpectedConditions.visibilityOfElementLocated(next_desired_element));
這篇關于如何修復未知錯誤:未處理的檢查器錯誤:“找不到具有指定 ID 的上下文"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!