Chromedriver未在Selenium测试中加载页面 [英] Chromedriver Not Loading Page in Selenium Tests

查看:126
本文介绍了Chromedriver未在Selenium测试中加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用chromedriver.exe进行异常处理,并在Selenium的帮助下编写了一些单元测试.当执行第一个使用chromedriver的测试时,浏览器将启动并通过测试.

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.

但是,对于以下所有所有使用chromedriver的测试,浏览器均无法成功导航到该网址.

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

浏览器启动,暂时在地址栏中显示字符 data; (就像在第一个有效的测试中一样),然后将正确的URL插入地址栏中.但是,该页面永远不会加载,并且您可以通过两个按钮 reload more在浏览器的主体/画布中获得标准的Chrome浏览器此网页不可用消息. .

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.

这是一个已知问题吗?

我正在使用以下版本:

硒:2.41.0.0
Chromedriver.exe:2.9.0.0
Visual Studio 2013:12.0.30110.00更新1

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

我的 ChromeTestDriver 类中具有以下 Initialize 方法,该方法将在Setup方法(也称为TestInitialize)中为所有测试调用:

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

以下异常被删除:

OpenQA.Selenium.NoSuchWindowException:没有这样的窗口:目标窗口已经关闭

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

让我知道是否可以提供更多信息.

Let me know if I can provide more info.

修改 我观察到,必须先运行IE测试,然后Chrome才会失败.通常,发生以下情况. Chrome测试运行正常.然后运行IE测试.之后,所有Chrome测试均失败.

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.

进一步编辑 另一个不寻常的方面是,一旦Selenium为网站染上了铬,我将再也无法在Chrome中加载该网站.也就是说,如果我在Chrome地址栏中手动输入网址,则会显示相同的空白页面.

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.

即使再奇怪一次,如果我运行Fiddler2(基本上是代理),Chrome也会变得干净.它再次起作用.

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

我确信代理不是问题,因为我的系统没有代理,并且从我的系统中卸载Fiddler2时也会出现相同的结果.

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.

位于不同大陆的客户也可以重现这些症状.我们使用GIT进行合作.因此,它不仅限于我的系统.

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.

Selenium Google用户组中的一个用户建议在一个非常简单的情况下(即,不作为测试框架的一部分)使用webdriver重现该错误.这是我为此创建的控制台应用程序的代码:

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

推荐答案

尝试此代码.这很简单,它将解决您的问题.

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

//关闭浏览器实例.请勿使用close()方法,否则它将无法正常运行,除了Firefox

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

driver.quit();

driver.quit();

关闭将关闭当前活动窗口,如果是最后一个窗口,则将执行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

这篇关于Chromedriver未在Selenium测试中加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆