java - 切换到 iframe 并单击元素不起作用并获得 StaleElementReferenceException
问题描述
我正在这个网站上练习:https ://dojotoolkit.org/reference-guide/1.9/dijit/layout/TabContainer-examples.html
使用以下方法单击编程嵌套选项卡部分中的运行按钮:
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:
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));
推荐阅读
- node.js - 使用 mongoexport 时连接 sasl 对话错误(身份验证错误)
- r - 在 geom_point 图中错误地绘制反应点
- ionic-framework - 波纹效果在滚动时触发
- flutter - 如何从 dart 文件中获取 Js 文件中的设备令牌?
- html - 无法在 Angular 9 中使用 *ngFor 在选择列表中显示选项
- vba - 我需要帮助在 word 中的两个标题之间复制内容 - vba
- c# - 在 BAPI 中设置表参数失败
- vue.js - Vuelidate、v-for 和子组件化
- c++ - 如何使用另一个模板化类的实例来模板化一个类?
- amazon-web-services - 假设已设置权限,我能否从我的 AWS 账户为不同的 AWS 账户创建 Athena 表?