在我的应用程序中,当我打开页面时,左侧显示选项卡列表。
默认情况下,一个选项卡是打开状态,其他选项卡是关闭状态,所以我正在寻找打开状态选项卡类名称并单击选项卡,它已关闭,然后必须给另一个选项卡 ID 才能打开。
执行代码时,我收到"过时的元素引用:元素未附加到页面文档"异常。
我也尝试过隐式等待选项。
任何人都可以帮助解决这个问题吗?
driver.manage().timeouts().implicitlyWait(1000,TimeUnit.SECONDS);
WebElement element5 = driver.findElement(By.className("TopItemActive"));
if(element5.isEnabled())
{
element5.click();
}
driver.manage().timeouts().implicitlyWait(2000,TimeUnit.SECONDS);
WebElement element6 = driver.findElement(By.id("id_16_cell"));
element6.click();
System.out.println("Tab opened");
我的猜测是你的标签是用JavaScript创建和删除的。Webdriver 的作用是下载网页并将其存储在实例中。如果由于javascript而更改了某些内容,则Webdriver并不总是知道它。
这可以作为一个简单的解决方案
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(by)));
我发现没有什么可做的。我发现抛出的异常并重试。所以我为"点击"创建了一个新功能
public String click(By by){
String text = "";
driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
boolean unfound = true;
int tries = 0;
while ( unfound && tries < 3 ) {
tries += 1;
try {
wait.until(ExpectedConditions.refreshed(ExpectedConditions.visibilityOfElementLocated(by)));
text = driver.findElement(by).click();
unfound = false;
logger.info("Click element "+stripBy(by));
} catch ( StaleElementReferenceException ser ) {
logger.error( "ERROR: Stale element exception. " + stripBy(by) );
} catch ( NoSuchElementException nse ) {
logger.error( "ERROR: No such element exception. " + stripBy(by)+"nError: "+nse );
} catch ( Exception e ) {
logger.error( e.getMessage() );
}
}
if(unfound)
Assert.assertTrue(false,"Failed to locate element by locator " + stripBy(by));
return text;
}