Alert doesn't close using Selenium WebDriver with Google Chrome.

 ̄綄美尐妖づ 提交于 2019-12-20 04:40:35

问题


I have the following Selenium script for opening alert on rediff.com:

public class TestC {
    public static void main(String[] args) throws InterruptedException, Exception {
        System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
        WebDriver driver = new ChromeDriver();   
        driver.get("http://www.rediff.com/");
        driver.findElement(By.xpath("//*[@id='signin_info']/a[1]")).click();
        driver.findElement(By.id("btn_login")).click();
        Thread.sleep(5000);
        Alert alert=driver.switchTo().alert();
        alert.accept();
    }
}

This very same script is working fine in Firefox and IE9, however using Google Chrome after opening the alert, rest of the code is not working. The main thing is that does not shows any exception, error or anything.

Please provide any solution as soon as possible. Thanks a lot!

Note: If we need to change any setting of browser or any thing please let me know.

Selenium version:Selenium(2) Webdriver
OS:Windows 7
Browser:Chrome
Browser version:26.0.1410.64 m

回答1:


I'm pretty sure your problem is a very common one, that's why i never advise using Thread.sleep(), since it does not guarantee the code will run only when the Alert shows up, also it may add up time to your tests even when the alert is shown.

The code below should wait only until some alert is display on the page, and i'd advise you using this one Firefox and IE9 aswell.

public class TestC {
    public static void main(String[] args) throws InterruptedException, Exception {
        System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
        WebDriver driver = new ChromeDriver();   
        WebDriverWait wait = new WebDriverWait(driver, 5);

        driver.get("http://www.rediff.com/");
        driver.findElement(By.xpath("//*[@id='signin_info']/a[1]")).click();
        driver.findElement(By.id("btn_login")).click();

        wait.until(ExpectedConditions.alertIsPresent());

        Alert alert = driver.switchTo().alert();
        alert.accept();
    }
}

Mostly all that is done here, is changing Thread.sleep(), for a condition that actually will only move forward on the code as soon a alert() is present in the page. As soon as someone does, it wil switch to it and accept.

You can find the Javadoc for the whole ExpectedConditions class here.




回答2:


Unfortunately AlertIsPresent doesn't exist in C# API http://selenium.googlecode.com/git/docs/api/dotnet/index.html

You can use something like this:

private static bool TryToAcceptAlert(this IWebDriver driver)
{
    try
    {
        var alert = driver.SwitchTo().Alert();
        alert.Accept();
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}


public static void AcceptAlert(this IWebDriver driver, int timeOutInSeconds = ElementTimeout)
{
    new WebDriverWait(driver, TimeSpan.FromSeconds(timeOutInSeconds)).Until(
        delegate { return driver.TryToAcceptAlert(); }
        );
}


来源:https://stackoverflow.com/questions/16437911/alert-doesnt-close-using-selenium-webdriver-with-google-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!