[英]Is it possible to handle multiple Pop-ups in a single step?
此頁面有一個按鈕警報框:單擊此處。 如果我們單擊此按鈕,它會顯示一個彈出窗口,當我們單擊“確定”按鈕時,它會顯示另一個彈出窗口。
我的代碼是:
driver.findElement(By.xpath("//*[@id='contact-form-2599']/form/button")).click();
driver.switchTo().alert().accept();
driver.switchTo().alert().accept();
我使用相同的代碼兩次單擊彈出窗口。
是否可以在一個步驟中處理多個彈出窗口?
您可以遍歷driver
可以關注的所有可能的命名警報:
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();
我希望這有幫助。
這也可以與 for 循環一起使用,
// 它設置為 2 個警報
for (int i=1; i<3; i++)
{
driver.switchTo().alert().accept();//Here it runs twice so two pop-ups can be accepted.
}
請檢查這個它也適用於我的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.