[英]Selenium driver.Url vs. driver.Navigate().GoToUrl()
Which is the preferred method to open a Url (and are there any differences behind the scenes between):哪个是打开 Url 的首选方法(两者之间是否存在任何差异):
driver.Url = "http://example.com";
or或者
driver.Navigate().GoToUrl("http://example.com");
Also, if the driver is already pointing at the same page, will setting the Url a second time cause the page to refresh?另外,如果驱动程序已经指向同一个页面,第二次设置 Url 会导致页面刷新吗?
ie IE
...
driver.Url = "http://example.com";
driver.Url = "http://example.com"; //does this reload the page?
...
FWIW I'm using the Chrome driver chromedriver.exe, but it doesn't appear to be a managed assembly (I tried opening it with ILSpy but no luck). FWIW 我正在使用 Chrome 驱动程序 chromedriver.exe,但它似乎不是托管程序集(我尝试用 ILSpy 打开它,但没有运气)。
Selenium is an open source framework, so please have a look at the source code here . Selenium 是一个开源框架,所以请看这里的源代码。
GoToUrl()
is defined in RemoteNavigator.cs : GoToUrl()
在RemoteNavigator.cs 中定义:
/// <summary>
/// Navigate to a url for your test
/// </summary>
/// <param name="url">String of where you want the browser to go to</param>
public void GoToUrl(string url)
{
this.driver.Url = url;
}
/// <summary>
/// Navigate to a url for your test
/// </summary>
/// <param name="url">Uri object of where you want the browser to go to</param>
public void GoToUrl(Uri url)
{
if (url == null)
{
throw new ArgumentNullException("url", "URL cannot be null.");
}
this.driver.Url = url.ToString();
}
So basically driver.Navigate().GoToUrl();
所以基本上
driver.Navigate().GoToUrl();
sets driver.Url
under the hood and I don't see a difference there.在引擎盖下设置
driver.Url
,我看不出有什么不同。
However, driver.Navigate().GoToUrl()
is more flexible, which allows sending either string
or Uri
as parameter types, while only string is allowed when setting through driver.Url
.但是,
driver.Navigate().GoToUrl()
更灵活,它允许将string
或Uri
作为参数类型发送,而通过driver.Url
设置时只允许driver.Url
。
To your second question, the source code shows that driver.Navigate().Refresh()
asks browsers to refresh, while driver.Url
tells browsers to navigate.对于你的第二个问题,源代码显示
driver.Navigate().Refresh()
要求浏览器刷新,而driver.Url
告诉浏览器导航。 So these two are fundamentally different.所以这两者是根本不同的。 For more details, please see Difference between Refresh and Navigate function in browser control?
详情请参见浏览器控件中刷新和导航功能的区别?
If you want to refresh the page, please use driver.Navigate().Refresh();
如果要刷新页面,请使用
driver.Navigate().Refresh();
Refresh()
is defined in RemoteNavigator.cs : Refresh()
在RemoteNavigator.cs 中定义:
/// <summary>
/// Refresh the browser
/// </summary>
public void Refresh()
{
// driver.SwitchTo().DefaultContent();
this.driver.InternalExecute(DriverCommand.Refresh, null);
}
driver.Url
is defined in RemoteWebDriver.cs : driver.Url
在RemoteWebDriver.cs 中定义:
public string Url
{
...
set
{
...
try
{
this.Execute(DriverCommand.Get, parameters);
}
...
}
}
Adding to Yi Zeng's answer, the difference between reloading and refreshing has to do with the driver information about the page.添加到 Yi Zeng 的回答中,重新加载和刷新之间的区别与有关页面的驱动程序信息有关。 If we re-assign driver.Url = url again, the memory will host this overridden string value one more time —regardless of being the same.
如果我们再次重新分配driver.Url = url ,内存将再次承载这个覆盖的字符串值——无论是否相同。 While refreshing has to do with displaying updated data.
而刷新与显示更新的数据有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.