我正在这个网站上练习: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));
关于java - 切换到 iframe 并单击元素不起作用并获得 StaleElementReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59003989/