[英]Waiting for Page to Load before accepting alert Selenium Webdriver
我正在尝试进行一些基本的Web爬网,并且希望能够接受(或关闭)屏幕上的任何警报,以便我可以访问页面元素。 之前,我通过等待警报来进行管理。
private void handleAlert(){
try{
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
Logger.log("Accepting alert~!~~~~!!!");
}catch(NoAlertPresentException e){
// Do nothing :D
}catch(UnhandledAlertException e){
Logger.writeError("An alert was not handled!");
}catch(TimeoutException e){
Logger.writeError("There was a timeout occurring handling alert");
}
}
}
但是,如果在前2秒钟内没有加载页面,然后再加载,则存在问题(我的页面加载超时为15秒)。 在这种情况下,存在警报,但是,我将不再尝试取消/接受它。
为了解决这个问题,我决定编写一些代码以等待页面加载后再尝试处理警报。 代码如下:
/**
* Load an Url in the chrome browser via Selenium driver
* @param url: The web address to be crawled
* @throws InterruptedException
*/
public boolean loadUrl(String url) throws InterruptedException{
try{
driver.get(url);
waitTillPageLoads();
handleAlert();
return true;
}catch(TimeoutException e){
//e.printStackTrace();
Logger.writeError("There was a timeout for the url: "+url);
return false;
}
}
/**
* Use webdriver expected conditions override to wait until
* DOM state is completed
*/
private void waitTillPageLoads(){
ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>()
{
public Boolean apply(WebDriver driver)
{
return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
}
};
Wait<WebDriver> wait = new WebDriverWait(driver,30);
wait.until(expectation);
}
Buuuuut,如果执行此操作,则会收到一个AlertNotHandledException,如下面的摘要堆栈跟踪所示:
Exception in thread "main" org.openqa.selenium.UnhandledAlertException: unexpected alert open
Session ID: 876c48f2d94d10fe30984ab94f32884e
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:164)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:577)
at src.com.chrome.TestChromeDriver$1.apply(TestChromeDriver.java:349)
at src.com.chrome.TestChromeDriver$1.apply(TestChromeDriver.java:1)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:238)
at src.com.chrome.TestChromeDriver.waitTillPageLoads(TestChromeDriver.java:353)
at src.com.chrome.TestChromeDriver.loadUrl(TestChromeDriver.java:364)
at src.com.chrome.TestChromeDriver.crawl(TestChromeDriver.java:308)
at src.com.chrome.TestChromeDriver.main(TestChromeDriver.java:132)
因此,如何等待页面加载完毕,然后尝试处理存在的javascript警报。 谢谢!
如果你知道你在做什么, 真的要绕过所有JavaScript警告和对话框不顾后果,你总是可以遵循这样的建议 :
((JavascriptExecutor) driver).executeScript("window.alert = function() {}; window.prompt = function() {return null}; window.confirm = function() {return true}");
您需要在每次页面加载后运行该代码,但它比尝试处理不可预测的可变行为要干净得多。
(如果要试用它,请打开浏览器的开发人员工具,将scriptlet粘贴到JS控制台中,然后确认alert('hi');
不执行任何操作。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.