If Selenium throws a StaleElementReferenceException it is usually (?) because the state of web page (DOM) Selenium "owns" has changed due to outside interference but I started to get it when I added frameToBeAvailableAndSwitchToIt
before a switch
statement. My code:
driver.findElement(By.xpath(xpDownArrow)).click();
WebElement iframeElement = driver.findElement(By.xpath(xpIframe));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath(xpIframe)));
driver.switchTo().frame(iframeElement);
Originally my code consisted just of line 1, 2 and 4, which worked most of the time but failed 10-20 % of the executions, and I therefore added line 2 but now I get the StaleElementReferenceException
everytime this code executes. I don't get that. Why would line 3 change the DOM? Isn't this the recommended way to perform a switch
?