如何在selenium webdriver中处理Javascript警报/弹出窗口 [英] How to handle Javascript Alert/pop up window in selenium webdriver
本文介绍了如何在selenium webdriver中处理Javascript警报/弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不确定selenium webdriver是否可以处理Javascript警报/弹出窗口。
I am not sure whether selenium webdriver can handle Javascript alert/pop-up window.
我有一个类似于
的场景1.用户上传xls文件并单击上传按钮
2.将显示警报/弹出窗口。单击窗口上的确定
I have a scenario like
1. User uploads a xls file and click on upload button
2. Alert/Pop-up window will be displayed . Click "OK" on window
能够自动执行上述方案,但在运行脚本时会显示警报/弹出窗口。
Am able to automate the above scenario but the Alert/pop-up window is displayed while running the scripts.
我们可以处理javascript警告/弹出窗口吗?
Is their anyway workaround that we can handle javascript alert/pop-up window?
推荐答案
你也可以尝试等待警报显示然后接受它。
下面是代码(点击上传按钮后) :
Below is the code for that (after the upload button is clicked):
try{
//Wait 10 seconds till alert is present
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
//Accepting alert.
alert.accept();
System.out.println("Accepted the alert successfully.");
}catch(Throwable e){
System.err.println("Error came while waiting for the alert popup. "+e.getMessage());
}
这篇关于如何在selenium webdriver中处理Javascript警报/弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文