我正在以下网站上练习: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.
答案 0 :(得分:0)
StaleElementReferenceException通常在元素尚未附加到DOM并尝试与之交互时抛出。您可以通过应用等待条件来解决它,在该条件下等待,直到元素准备好可单击为止。
wait.until(ExpectedConditions.elementToBeClickable(WebElement));
答案 1 :(得分:0)
我编写了一个通用包装器来处理 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;
}
};
}
你可以这样称呼它
WebDriverWait wait = new WebDriverWait(driver, waitTimeOutInSeconds, pollTimeOutInMillis);
wait.until(frameToBeAvailableAndSwitchToIt(iFrameWebElement));