简体   繁体   English

Selenium IWebDriver Navigate()。GoToUrl()没有输入url或导航到页面

[英]Selenium IWebDriver Navigate().GoToUrl() not entering url or navigating to page

I'm creating a new test project and can't think of anything else to try to resolve my issue. 我正在创建一个新的测试项目,并且无法考虑其他任何事情来尝试解决我的问题。 I have another Selenium project that works as expected, and have compared the two to find differences. 我有另一个Selenium项目按预期工作,并将两者进行比较以发现差异。 But first, the main issue I'm running into. 但首先,我遇到的主要问题。 In the setup I call Driver.Navigate().GoToUrl("www.test.com"); 在设置中我调用Driver.Navigate()。GoToUrl(“www.test.com”); No exception is thrown or anything else that would indicate there is a problem, the flow just moves to the next statement, but no Url is entered into the navigation bar, and so obviously the driver never navigates to any page. 没有抛出任何异常或任何其他表明存在问题的流程,流程只会移动到下一个语句,但是没有Url进入导航栏,因此显然驱动程序永远不会导航到任何页面。 The driver does start up a new firefox instance, but it stays blank. 驱动程序确实启动了一个新的firefox实例,但它仍然是空白的。

When I compare this new project against an already working project, they seem identical for the most part. 当我将这个新项目与已经在工作的项目进行比较时,它们在大多数情况下看起来完全一致。 Both projects have ...\\packages\\Selenium.WebDriver.2.37.0 & ...\\packages\\Selenium.Support.2.37.0 installed using NuGet package manager. 这两个项目都使用NuGet包管理器安装了... \\ packages \\ Selenium.WebDriver.2.37.0&... \\ packages \\ Selenium.Support.2.37.0。 Both projects have identical references to the project in the .csproj file --Working project ... ....\\packages\\Selenium.WebDriver.2.37.0\\lib\\net40\\WebDriver.dll False ....\\packages\\Selenium.Support.2.37.0\\lib\\net40\\WebDriver.Support.dll 这两个项目对.csproj文件中的项目都有相同的引用 - 工作项目...... \\ packages \\ Selenium.WebDriver.2.37.0 \\ lib \\ net40 \\ WebDriver.dll False .... \\ packages \\ Selenium.Support.2.37.0 \\ LIB \\ net40 \\ WebDriver.Support.dll

--Unworking project ... ..\\packages\\Selenium.WebDriver.2.37.0\\lib\\net40\\WebDriver.dll ..\\packages\\Selenium.Support.2.37.0\\lib\\net40\\WebDriver.Support.dll --Unworking project ... .. \\ packages \\ Selenium.WebDriver.2.37.0 \\ lib \\ net40 \\ WebDriver.dll .. \\ packages \\ Selenium.Support.2.37.0 \\ lib \\ net40 \\ WebDriver.Support.dll

Neither project does anything else. 这两个项目都没有做任何事情。 The constructor for both simply calls: var WebDriver = new FirefoxDriver(); 两者的构造函数只需调用:var WebDriver = new FirefoxDriver();

When I examine the WebDriver object, the only difference I can see is the WindowsHandles property. 当我检查WebDriver对象时,我能看到的唯一区别是WindowsHandles属性。 The working project has: WindowsHandles Count = 1 工作项目具有:WindowsHandles Count = 1

The non-working project has: WindowsHandles {System.Collections.ObjectModel.ReadOnlyCollection} 非工作项目具有:WindowsHandles {System.Collections.ObjectModel.ReadOnlyCollection}

I have no idea why they're different but as it's the only difference I can find, I'm thinking maybe that's the problem but I have no idea if that's really the case or how I'd fix it. 我不知道他们为什么会有所不同,但因为这是我能找到的唯一区别,我想也许这就是问题,但我不知道是不是真的如此,或者我是如何解决它的。 I've added a try/catch block around the WebDriver.Navigate().GoToUrl() and no exception is being caught. 我在WebDriver.Navigate()。GoToUrl()周围添加了一个try / catch块,没有异常被捕获。
Both projects target the .NET 4.0 framework. 这两个项目都针对.NET 4.0框架。 Any help is greatly appropriated. 任何帮助都非常适合。

I figured it out right after I finally broke down and decided to post on StackOverflow. 在我终于崩溃并决定在StackOverflow上发布之后,我立刻意识到了这一点。 My issue was IWebDrivers insists upon a passed in URL beginning with http . 我的问题是IWebDrivers坚持以http开头的传入URL。 Once I prefixed my URL with that, it worked. 一旦我用我的URL作为前缀,它就有效了。

Here is a simple example how to do so: 这是一个简单的例子:

  1. Add NuGet package Selenium.WebDriver 添加NuGet包Selenium.WebDriver

  2. Download chromedriver ( https://sites.google.com/a/chromium.org/chromedriver/ ) or any other driver from http://www.seleniumhq.org/download/ (section Third Party Browser Drivers NOT DEVELOPED by seleniumhq) http://www.seleniumhq.org/download/下载chromedriver( https://sites.google.com/a/chromium.org/chromedriver/ )或任何其他驱动程序(seleniumhq未开发的第三方浏览器驱动程序部分)

  3. Copy the file to your project directory in Visual Studio and set the following properties for the file: 将文件复制到Visual Studio中的项目目录,并为该文件设置以下属性:

    Build Action = None; 构建动作=无; Copy to Output Directory = Copy if newer 复制到输出目录=如果更新则复制

Once installed, try running the following example code: 安装后,尝试运行以下示例代码:

var driver = new ChromeDriver();
var navigate = driver.Navigate();
navigate.GoToUrl("http://www.microsoft.com"); //works
navigate.GoToUrl("www.microsoft.com"); //does not work

Good luck! 祝好运!

Also

navigate.GoToUrl("http:\\www.microsoft.com"); // "\\" instead of "//", wont work
navigate.GoToUrl("http://www.microsoft.com"); // that one works perfectly.

with backslashes instead of forward slashes wont work either... I've just spend about an hour, figuring this out. 用反斜杠代替正斜杠也不会工作......我只花了大约一个小时,搞清楚这一点。

URL address, correcly must be with forward slashes. URL地址,必须使用正斜杠。

Just concatenate on url "http://" and this problema is solved. 只需在URL“http://”上连接,这个问题就解决了。 oO I don't know why this error, but's solved. oO我不知道为什么会出现这个错误,但是已经解决了。 Just do it ! 去做就对了 !

My issue with Navigate() not working was when driver focus is on the wrong tab (most commonly, if you've already opened another tab by clicking a "_blank" link on the current tab) and you are calling Navigate().GoToUrl(). Navigate()无法正常工作的问题是当驱动程序焦点位于错误的选项卡上时(最常见的情况是,如果您已通过单击当前选项卡上的“_blank”链接打开了另一个选项卡)并且您正在调用Navigate()。GoToUrl ()。

Then you should be selecting the tab again in which you will change the url, like this: C# example 然后你应该再次选择你将更改网址的标签,如下所示:C#示例

var tabs = this.browser.WindowHandles;
this.browser.SwitchTo().Window(tabs[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM