How to fix unknown error: unhandled inspector error: quot;Cannot find context with specified idquot;(如何修复未知错误:未处理的检查器错误:“找不到具有指定 ID 的上下文)
问题描述
以下代码偶尔会抛出 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 的上下文"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何修复未知错误:未处理的检查器错误:“找不到具有指定 ID 的上下文"
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01