3

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?

2
  • Update your question with the dom structure of your page. Commented May 20, 2018 at 5:10
  • Switch to the iframe firstly, then find the element and click on it. Commented May 20, 2018 at 6:46

1 Answer 1

3

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)); 
Sign up to request clarification or add additional context in comments.

2 Comments

Works, but could frameToBeAvailableAndSwitchToIt return because the old iframe is still available?
@user3561614 As the name implies frameToBeAvailableAndSwitchToIt involves the waiter and the switching both. So won't return.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.