Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use relative paths with the webdriver.Navigate().GotoUrl()?

driver.Navigate().GoToUrl("/") sets the location to "/" instead of "http://www.domain.com/"

another example would be

driver.Navigate().GoToUrl("/view1") sets the location to "/view1" instead of "http://www.domain.com/view1"

Either example would cause the browser to return with address isn't valid.

like image 915
ton.yeung Avatar asked May 15 '13 22:05

ton.yeung


2 Answers

you can use a Java URI to calculate a path relative to the current uri or domain:

import java.net.URI;

driver.get("http://example.org/one/");

// http://example.org/one/two/
driver.get(new URI(driver.getCurrentUrl()).resolve("two/").toString());

// http://example.org/one/two/three/?x=1
driver.get(new URI(driver.getCurrentUrl()).resolve("three/?x=1").toString());

// http://example.org/one/two/three/four/?y=2
driver.get(new URI(driver.getCurrentUrl()).resolve("./four/?y=2").toString());

// http://example.org/one/two/three/five/
driver.get(new URI(driver.getCurrentUrl()).resolve("../five/").toString());

// http://example.org/six
driver.get(new URI(driver.getCurrentUrl()).resolve("/six").toString());

If you're able to calculate the url without using getCurrentUrl(), it might make your code more readable.

like image 200
Alexander Taylor Avatar answered Nov 15 '22 23:11

Alexander Taylor


The solution now is to use:

driver.Navigate().GoToRelativePath("/view1");

and you will be navigated within the same domain.

UPDATE: This was valid in Selenium WebDriver 2.42 but does not seem to be listed in 3.1 the solution would be

driver.Navigate().GoToUrl(baseUrl + "/view1")

like image 39
Michael Nakayama Avatar answered Nov 15 '22 21:11

Michael Nakayama