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

标签 java selenium iframe staleelementreferenceexception

我正在这个网站上练习:https://dojotoolkit.org/reference-guide/1.9/dijit/layout/TabContainer-examples.html

  1. 使用以下方法单击“编程嵌套”选项卡部分中的“运行”按钮:

        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. 弹出屏幕需要一段时间才能加载。然后我尝试单击选项卡 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.


最佳答案

当元素尚未附加到 DOM 并且您尝试与其交互时,通常会抛出 StaleElementReferenceException。您可以通过应用等待条件来解决这个问题,直到元素准备好可点击为止。

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

关于java - 切换到 iframe 并单击元素不起作用并获得 StaleElementReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59003989/

相关文章:

java - 正则表达式 (Java) 异常 - 寻求解释

java - Netbeans 中的 Vaadin 项目执行

java - Android 日历 getFirstDayOfWeek 错误

python - Selenium:get_attribute 一个条件

javascript - 在不同域上重定向 iframe 的父级 - 替代逻辑

java - JTable 排序在单元格中没有好的值(value)

java - 我应该检查文本字段是否有文本然后清除它还是直接清除它?

java - 使用 jUnit、Selenium Webdriver 打印有关测试失败的有意义的消息并继续执行脚本

javascript - javascript 如何找出 (i)frame 层次结构?

javascript - 如何检测 iframe 调整大小?