selenium - Selenium: Alert fails to execute before driver closes
问题描述
I have a code like this:
public class classTest {
WebDriver webDriver;
WebDriverWait wait;
ExpectedCondition<Boolean> pageLoadCondition = new
ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver)
.executeScript("return document.readyState")
.equals("complete");
}
};
@BeforeEach
public void init() {
webDriver = new FirefoxDriver();
wait = new WebDriverWait(webDriver, 10);
}
@AfterEach
public void quit() {
webDriver.quit();
}
@Test
public void Test1() {
...
}
@Nested
class classTest2 {
@BeforeEach
public void create() {
...
}
@AfterEach
public void closeAlert() {
webDriver.get("https://...");
...
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
webDriver.switchTo().alert().accept();
}
@Test
public void Test2() {
...
}
...
When Test2()
completes, the closeAlert()
method is executed.
An alert appears on the page, but accept()
fires 50% of the time. My guess is that it does not have time to fire, because the quit()
method is immediately executed, which closes the browser.
I've triedwait.until(pageLoadCondition);
after alert, but that doesn't help.
How do I wait for the alert confirmation to complete?
解决方案
您可以等待在网页上显示/等实际的 WebElement。
如果您没有特定元素,则可以使用几毫秒的Thread.sleep
.
可能不是理想的解决方案,但只要您只需要等待,别无其他,它应该可以解决问题。
推荐阅读
- c# - 在 C# 8 中使用它进行赋值之前,如何检查 Null 的对象属性?
- postgresql - 无法在 postgres 容器中运行脚本
- reactjs - 如何实现 react-native-data-table?
- python - 使用 Python 在 Twitter 上发布 Selenium。我无法获得带有任何硒定位元素的按钮
- c - Can't get while loop to work 2 times? (C)
- spring - Spring MVC form:select selection from database issue
- jpa - 使用带有 jpa NamedStoredProcedureQuery 的 sybase 存储过程
- javascript - 如何使用 javascript 将带有数组的对象加入到单个数组中?
- python - Python 使用带有 for 循环的三元运算符
- php - php glob - 只获取文件名,没有扩展名