This page有一个按钮 Alert Box : Click Here。如果我们单击此按钮,它会显示一个弹出窗口,当我们单击 OK 按钮时,它会显示另一个弹出窗口。
我的代码是:
driver.findElement(By.xpath("//*[@id='contact-form-2599']/form/button")).click();
driver.switchTo().alert().accept();
driver.switchTo().alert().accept();
我两次使用相同的代码来单击弹出窗口。
是否可以一步处理多个弹出窗口?
最佳答案
您可以遍历 驱动程序
可以关注的所有可能的命名警报:
function closeAlerts() {
// this aussmes that there is at least one alert
try {
let currentAlert = driver.switchTo().alert();
}
catch (err) {
console.log(err);
return;
}
// loop through the alerts and accept them
while (currentAlert && currentAlert.accept instanceof Function) {
currentAlert.accept();
try {
currentAlert = driver.switchTo().alert();
}
catch (err) {
console.log(err);
return;
}
}
}
closeAlerts();
希望对您有所帮助。
关于javascript - 是否可以一步处理多个弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46905845/