25

Which is the preferred method to open a Url (and are there any differences behind the scenes between):

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?

i.e.

...
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).

2 Answers 2

39

Selenium is an open source framework, so please have a look at the source code here.

GoToUrl() is defined in 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(); sets driver.Url under the hood and I don't see a difference there.

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.


To your second question, the source code shows that driver.Navigate().Refresh() asks browsers to refresh, while driver.Url tells browsers to navigate. 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();

Refresh() is defined in 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:

public string Url
{
    ...

    set
    {
        ...

        try
        {
            this.Execute(DriverCommand.Get, parameters);
        }
        ...
    }
}
3
  • Looking at the source code you provided it looks like the answer to my second question is yes - the code always tells the driver to reload the page, even if the Url hasn't changed.
    – steve cook
    Sep 12, 2014 at 3:13
  • @steve: Yeah, it depends on how you define words "reload" and "refresh". ;)
    – Yi Zeng
    Sep 12, 2014 at 11:23
  • 1
    driver.Navigate().GoToUrl() only set operation .But driver.Url set and get current URL
    – arman
    Dec 28, 2019 at 12:09
0

Adding to Yi Zeng's answer, the difference between reloading and refreshing has to do with the driver information about the page. If we re-assign driver.Url = url again, the memory will host this overridden string value one more time —regardless of being the same. While refreshing has to do with displaying updated data.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.