Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在接受警报Selenium Webdriver之前等待页面加载_Javascript_Java_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

Javascript 在接受警报Selenium Webdriver之前等待页面加载

Javascript 在接受警报Selenium Webdriver之前等待页面加载,javascript,java,selenium,selenium-webdriver,webdriver,Javascript,Java,Selenium,Selenium Webdriver,Webdriver,我试图做一些基本的网页爬行,并希望能够接受(或解除)屏幕上的任何警报,以便我可以访问页面元素。早些时候,我通过执行预期的等待警报来管理这一点 private void handleAlert(){ try{ WebDriverWait wait = new WebDriverWait(driver, 2); wait.until(ExpectedConditions.alertIsPresent()); Alert alert = driv

我试图做一些基本的网页爬行,并希望能够接受(或解除)屏幕上的任何警报,以便我可以访问页面元素。早些时候,我通过执行预期的等待警报来管理这一点

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);
    }

那么,如何等待页面加载,然后尝试处理javascript警报(如果存在)。谢谢

如果您知道自己在做什么,并且不管后果如何,都必须绕过所有JavaScript警报和对话框,那么您始终可以:

((JavascriptExecutor)驱动程序).executeScript(“window.alert=function(){};window.prompt=function(){return null};window.confirm=function(){return true}”)

您需要在每次页面加载后运行它,但它肯定比尝试处理不可预测的、可变的行为更干净


(如果您想试用它,请打开浏览器的开发工具,将scriptlet粘贴到JS控制台,然后验证
警报('hi');
不起任何作用。)

如果您知道自己在做什么,并且不管后果如何都必须绕过所有JavaScript警报和对话框,您可以始终:

((JavascriptExecutor)驱动程序).executeScript(“window.alert=function(){};window.prompt=function(){return null};window.confirm=function(){return true}”)

您需要在每次页面加载后运行它,但它肯定比尝试处理不可预测的、可变的行为更干净


(如果您想试用它,请打开浏览器的开发工具,将scriptlet粘贴到JS控制台中,然后验证
alert('hi');
不起任何作用。)

我刚刚尝试了这一点(用您的代码片段替换了handleAlert函数),它返回了一个未处理的AlertException xD。我觉得这很有趣,因为这正是应该避免的哈哈哈。我看不到你正在加载的页面,也不知道这些警报应该发生在哪里,但是如果你在执行之前等待页面加载,那可能太晚了。您应该在请求页面后尽快运行它,以最大限度地减少显示任何警报/对话框的机会。我知道这是一个非常老的问题,但我记得这确实有效。所以我接受了答案,我只是尝试了一下(用你的代码片段替换我的handleAlert函数),它返回了一个未处理的AlertException xD。我觉得这很有趣,因为这正是应该避免的哈哈哈。我看不到你正在加载的页面,也不知道这些警报应该发生在哪里,但是如果你在执行之前等待页面加载,那可能太晚了。您应该在请求页面后尽快运行它,以最大限度地减少显示任何警报/对话框的机会。我知道这是一个非常老的问题,但我记得这确实有效。所以我接受了答案。
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)