8

driver.Navigate().GoToUrl("/") 将位置设置为“/”而不是“ http://www.domain.com/

另一个例子是

driver.Navigate().GoToUrl("/view1") 将位置设置为“/view1”而不是“ http://www.domain.com/view1

这两个示例都会导致浏览器返回地址无效。

4

3 回答 3

5

您可以使用 Java URI 来计算相对于当前 uri 或域的路径:

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());

如果您能够在不使用 getCurrentUrl() 的情况下计算 url,它可能会使您的代码更具可读性。

于 2016-06-21T17:35:27.277 回答
3

现在的解决方案是使用:

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

您将在同一个域中导航。

更新: 这在 Selenium WebDriver 2.42 中有效,但似乎没有在 3.1 中列出,解决方案是

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

于 2016-01-25T23:33:56.703 回答
0

当它们都具有相同的域时,这可能是导航到特定 url 的最短方法:

private String baseUrl = "http://www.domain.com/";

[...]

driver.get(baseUrl + "url");

driver.get(String url) 等价于 driver.navigate().to(String url)。

于 2013-08-07T13:50:00.513 回答