How to use relative paths using webdriver.Navigate (). GotoUrl ()?

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

another example:

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

Any example will cause the browser to return with the address invalid.

+4
source share
3 answers

Now the solution is to use:

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

and you will be moving within the same domain.

UPDATE: This was valid in Selenium WebDriver 2.42, but it does not seem to be indicated in 3.1, the solution will be

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

+2
source

you can use the Java URI to calculate the 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 can calculate the url without using getCurrentUrl (), this can make your code more readable.

+2
source

This is probably the shortest way to go to a specific URL when they all have the same domain:

 private String baseUrl = "http://www.domain.com/"; [...] driver.get(baseUrl + "url"); 

driver.get (String url) is equivalent to driver.navigate (). to (String url).

+1
source

Source: https://habr.com/ru/post/1481047/


All Articles