简体   繁体   English

Selenium driver.Url 与 driver.Navigate().GoToUrl()

[英]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()更灵活,它允许将stringUri作为参数类型发送,而通过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.UrlRemoteWebDriver.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.

相关问题 C#将Selenium与Chrome Portable和driver.Navigate()。GoToUrl结合使用 - C# Using Selenium with Chrome Portable and driver.Navigate().GoToUrl Selenium webdriver - 驱动程序。在同一页面上导航 - Selenium webdriver - driver.Navigate on same page 在 Main 函数以外的任何类中使用 Selenium IWebDriver 从 driver.Url 获取“System.NullReferenceException”。 C# - Getting 'System.NullReferenceException' from driver.Url using Selenium IWebDriver in any class other than the Main function. C# Selenium IWebDriver Navigate()。GoToUrl()没有输入url或导航到页面 - Selenium IWebDriver Navigate().GoToUrl() not entering url or navigating to page Selenuim driver.Navigate()。back()在浏览单词和前进时会中断HTML DOM - Selenuim driver.Navigate().back() breaks the HTML DOM when navigating back words and forwards CUDA 驱动程序 API 与 CUDA 运行时 - CUDA Driver API vs. CUDA runtime 使用C#将驱动程序导航到硒中新打开的窗口 - Navigate driver to new opened window in selenium with C# Chromedriver 失败 - OpenQA.Selenium.Remote.RemoteNavigator.GoToUrl(字符串网址) - Chromedriver fails - OpenQA.Selenium.Remote.RemoteNavigator.GoToUrl(String url) VS/Selenium - Object 引用未设置为具有 null 驱动程序的 object 实例 - VS/Selenium - Object reference not set to an instance of an object with null driver Firefox硒驱动程序 - Firefox driver for selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM