如何修复未知错误:未处理的检查器错误:“无法找到具有指定ID的上下文"; [英] How to fix unknown error: unhandled inspector error: "Cannot find context with specified id"

查看:387
本文介绍了如何修复未知错误:未处理的检查器错误:“无法找到具有指定ID的上下文";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码偶尔会抛出org.openqa.selenium.WebDriverException.

WebElement element = driver.findElement(by);
element.click();
(new WebDriverWait(driver, 4, 100)).until(ExpectedConditions.stalenessOf(element));

页面看起来像这样(是<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"}. elementiframe的一部分,单击会导致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,然后针对stalenessOf()上一个元素或下一个所需元素引入 WebDriverWait ,如下所示:

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的上下文";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆