简体   繁体   English

切换到 iframe 并单击元素不起作用并获得 StaleElementReferenceException

[英]Switch to iframe and click on element doesn't work and get StaleElementReferenceException

I'm doing practise on this website: https://dojotoolkit.org/reference-guide/1.9/dijit/layout/TabContainer-examples.html我正在这个网站上练习: https : //dojotoolkit.org/reference-guide/1.9/dijit/layout/TabContainer-examples.html

  1. Click on Run button in Programmatic Nested tabs section with following method:使用以下方法单击“程序化嵌套选项卡”部分中的“运行”按钮:

     WebElement productElement = null; List<WebElement> productElements= driver.findElements(By.cssSelector(div.section)); for(int i=0;i<productElements.size();i++) { String text = productElements.get(i).findElement(By.tagName("h2")).getText(); if (text.equalsIgnoreCase(tabName)){ productElement = productElements.get(i); break; } } return productElement; } public void clickRunButton(String tabName) { WebElement programmaticNestedtabs = findTab(tabName); WebElement runButton = programmaticNestedtabs.findElement(By.cssSelector("a.CodeGlassMiniRunner")); runButton.click(); }
  2. The pop-up screen takes a while to load.弹出屏幕需要一段时间才能加载。 Then I try to click on Tab 2:然后我尝试单击选项卡 2:

WebDriverWait wait = new WebDriverWait(driver, 50);
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
dialog.findElement(By.xpath("//div[@class='dijitTabListWrapper dijitTabContainerTopNone dijitAlignCenter']//div[2]")).click();```

I got the StaleElementReferenceException when I run the code.


StaleElementReferenceException is generally thrown when element is not yet attached to the DOM and you are trying to interact with it. StaleElementReferenceException 通常在元素尚未附加到 DOM 并且您尝试与其交互时抛出。 You can get get around it by applying the wait condition where it waits until the element is ready to be clickable.您可以通过应用等待条件来解决它,它会等待元素准备好可点击。

wait.until(ExpectedConditions.elementToBeClickable(WebElement));

I have written a general wrapper to handle similar exceptions in case of iFrame我已经编写了一个通用包装器来处理 iFrame 的类似异常

private ExpectedCondition<Boolean> frameToBeAvailableAndSwitchToIt(final WebElement var0) {
    return new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver var1) {
            try {
                if (var0).isDisplayed()) {
                    var1.switchTo().frame(var0);
                    return true;
                }
            } catch (NoSuchFrameException var3) {
                return false;
            } catch (NoSuchElementException var4) {
                return false;
            } catch (StaleElementReferenceException var5) {
                return false;
            }
            return false;
        }

        public String toString() {
            return "frame to be available: " + var0;
        }
    };
}

You can call this as below你可以这样称呼它

WebDriverWait wait = new WebDriverWait(driver, waitTimeOutInSeconds, pollTimeOutInMillis);
    wait.until(frameToBeAvailableAndSwitchToIt(iFrameWebElement));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM