java - 动态等待元素变为可点击不起作用
问题描述
我写了一个函数如下
public static WebElement waitForElementToBeClickable(WebDriver driver, WebElement webElement, int seconds) {
WebDriverWait wait = new WebDriverWait(driver, seconds);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(webElement));
element.click();
return element;
}
现在我正在导入这个函数并使用它如下
private String loginButton = "btnActive";
private String home = "//a[@id='pt1:_UIShome']";
WebElement login = driver.findElement(By.id(loginButton));
libraryUtils.waitForElements.waitForElementToBeClickable(driver, login, 20).click();
WebElement homeButton = driver.findElement(By.xpath(home));
libraryUtils.waitForElements.waitForElementToBeClickable(driver, homeButton, 20).click();
单击登录按钮,但在转到下一页后,我收到以下错误 -
org.openqa.selenium.StaleElementReferenceException:过时的元素引用:元素未附加到页面文档
Xpath 是正确的,我已经检查过了。当我使用 时element.click()
,这也可以,但那将需要我手动输入睡眠时间,这就是我正在做的事情。我希望这是动态的。感谢任何帮助或建议。
解决方案
有时 JS 还没有完成运行,如果你尝试与web 元素或web 元素交互,你会看到staleElemenent exception
.
但是,以下解决方案可能对您有用:
new WebDriverWait(driver, 10).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@id='pt1:_UIShome']")));
driver.findElement(By.xpath("//a[@id='pt1:_UIShome']")).click();
推荐阅读
- azure-web-app-service - IDX21323 错误,有时在 azure b2c 中的 URL 中循环
- macos - Safari 是否仅将完整加载的页面放入历史记录中?
- c# - 尝试使用 JSON.NET 使用周围的 [] 字符反序列化 JSON
- excel - Export-Csv .csv 文件格式不正确
- user-interface - 托管在 IIS8 上时,Silverlight 无法正确呈现
- r - 如何检查一个数字序列是否是斐波那契序列并获得 R 中的下一个值?
- java - 如何比较学生对象
- microsoft-graph-api - 从 Graph API 中删除联系人
- android - Android Studio 3.5:由于包含 Kotlin 模块,不支持新的 Gradle Sync
- amazon-web-services - 获取 AWS Elastic IP 的letsEncrypt CA 证书