Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

java - How to fix unknown error: unhandled inspector error: "Cannot find context with specified id"

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...