Chromedriver Not Loading Page in Selenium Tests

2019-06-15 14:12发布

I’m experiencing an exception scenario using chromedriver.exe with some unit tests that I am writing with the help of Selenium. When the first test that uses the chromedriver executes, the browser fires up and the test passes.

However, for all the following tests that use the chromedriver, the browser does not successfully navigate to the URL.

The browser fires up, momentarily the characters data; appear in the address bar (as it did in the first test that worked), then the correct URL is inserted into the address bar. However, the page never loads and you get the standard chrome This webpage is not available message in the body/canvas of the browser with the two buttons reload and more.

Is this a known issue?

I am using the following versions:

Selenium: 2.41.0.0
Chromedriver.exe: 2.9.0.0
Visual Studio 2013: 12.0.30110.00 Update 1

I have the following Initialize method in my ChromeTestDriver class which gets invoked in the Setup method (aka TestInitialize) for all tests:

ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService(DriverPath);
var chromeOptions = new ChromeOptions();

chromeDriverService.Port = DriverPort; // 9999 - this is the port for the driver, not the webpage 

webDriver = new ChromeDriver(chromeDriverService, chromeOptions);
webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
webDriver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(10)); 

I have the following code in the Cleanup method (aka Teardown) for all tests:

TestDriver.CloseWindow();  
TestDriver.Quit();

The following exception gets chucked:

OpenQA.Selenium.NoSuchWindowException: no such window: target window already closed

Let me know if I can provide more info.

Edit I've observed that an IE test has to run first before Chrome fails. Generally, the following happens. A Chrome test runs fine. Then an IE test runs. Subsequent to that, all Chrome tests fail.

Further Edit Another unusual aspect to this is that once chrome has been sullied by Selenium for the website, I can no longer load the website in Chrome at all. That is, if I manually type in the URL in Chrome’s address bar, the same empty page is displayed.

Even weirder again, if I run up Fiddler2 (which is basically a proxy), Chrome becomes unsullied. It works again.

I’m confident proxies aren’t the issue as my system has no proxy and the same result occurs when Fiddler2 has been uninstalled from my system.

The symptoms are also reproducible by my client who is located on a different continent. We collaborate using GIT. So it is not confined to my system.

A user on the Selenium Google users group suggested reproducing the bug using the webdriver in a really simple scenario (i.e. not as part of a testing framework). Here is the code for the console app which I created to do that:

private static string Url = "http://localhost:5556";

static void Main(string[] args)
{
    var chromeWebDriver = GetChromeWebDriver();
    var nav = chromeWebDriver.Navigate();
    nav.GoToUrl(Url);
    Thread.Sleep(3000);
    chromeWebDriver.Quit();
    chromeWebDriver.Dispose();

    var iedriver = GetIeDriver();
    var nav1 = iedriver.Navigate();
    nav1.GoToUrl(Url);
    iedriver.Quit();
    iedriver.Dispose();

    var chromeWebDriver2 = GetChromeWebDriver();
    var nav2 = chromeWebDriver2.Navigate();
    nav2.GoToUrl(Url);
    chromeWebDriver2.FindElement(By.LinkText("Login")).Click();

    System.Threading.Thread.Sleep(2000);

    chromeWebDriver2.Quit();
    chromeWebDriver2.Dispose();

    Console.ReadLine();
}

private static IWebDriver GetIeDriver()
{
    InternetExplorerDriverService internetExplorerDriverService =
        InternetExplorerDriverService.CreateDefaultService(
            @"H:\BW\packages\Selenium.WebDriver.IEDriver.2.41.0.1\content");
    InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions();
    internetExplorerDriverService.Port = 9999;
    IWebDriver webdriver = new InternetExplorerDriver(internetExplorerDriverService, internetExplorerOptions);
    return webdriver;
}

private static IWebDriver GetChromeWebDriver()
{
    var chromeDriverService =
        ChromeDriverService.CreateDefaultService(
            @"H:\BW\packages\Selenium.WebDriver.ChromeDriver.2.10.0.0\content");
    var chromeOptions = new ChromeOptions();
    chromeDriverService.Port = 7777;
    IWebDriver chromeWebDriver = new ChromeDriver(chromeDriverService, chromeOptions);
    return chromeWebDriver;
}

6条回答
我命由我不由天
2楼-- · 2019-06-15 14:36

I had the same issue, and it was because I was using the wrong ChromeDriver. It's better if you download the driver from third party browser drivers from the official site http://www.seleniumhq.org/download/.

查看更多
ら.Afraid
3楼-- · 2019-06-15 14:37

You can directly use TestDriver.Quit(); as it would serve the purpose, the window would automatically be closed, no need include an extra method in your code i.e the TestDriver.CloseWindow(); if you are not dealing with multiple open windows.

查看更多
爷的心禁止访问
4楼-- · 2019-06-15 14:38

Try this code. It is simple and it will solve your problem.

if(browserType.equals("googleChrome")==true)
{

    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\autoItfiles\\chromedriver.exe");
    driver = new ChromeDriver();

    Report.info("Google chrome browser is opened ");                
}

// To close browser instance. Do not use close() method mostly it does not works other then Firefox

driver.quit();

Close will shut the current active window and if it is the last window will then perform a quit(),

If your test has failed that session is probably dead, so when you call a close it doesn't know where to send the command and doesn't do anything.

Quit will shut down all clients if there are no active sessions so if you send a quit and have no active sessions it will just clean up

查看更多
仙女界的扛把子
5楼-- · 2019-06-15 14:46

Are you specifying the same port for all your drivers? If so, that is most likely what is causing the issue. In your scenario, the IEDriverServer is listening on port 9999. Then, when you launch the ChromeDriver, its also trying to use port 9999, but can't, because its already taken, so you get the page not found error.

Try letting the drivers retrieve their own port, which will automatically find an available port to use, and then see what happens.

查看更多
萌系小妹纸
6楼-- · 2019-06-15 14:51

Try with Chromedriver v2.7

Because I too faced the same issue

查看更多
\"骚年 ilove
7楼-- · 2019-06-15 14:55

This solved my problem by indicating the port number since ChromeDriver started with statement only local connections allowed and indicating it uses a certain port.

public static void main(String ... args){
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\2.16\\chromedriver.exe");
    ChromeDriverService.Builder builder =  new ChromeDriverService.Builder();
    ChromeDriverService srvc = builder.usingDriverExecutable(new File("C:\\chromedriver\\2.16\\chromedriver.exe"))
                        .usingPort(9515).build();
    try {
        srvc.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //Execute your test-script commands
    WebDriver driver = new ChromeDriver(srvc);
    driver.get("http://www.google.com");
    WebElement searchBox = driver.findElement(By.name("q"));
    searchBox.sendKeys("Chrome Driver");
}
查看更多
登录 后发表回答