How to fix unknown error: unhandled inspector error: “Cannot find context with specified id”

一曲冷凌霜 提交于 2020-03-23 02:07:56

问题


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));

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>

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?


回答1:


This error message...

unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot find context with specified id"}

...implies that the WebDriver instance was unable to locate the desired element.

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));


来源:https://stackoverflow.com/questions/50429994/how-to-fix-unknown-error-unhandled-inspector-error-cannot-find-context-with-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!