如何在webdriver.Navigate()中使用相对路径.GotoUrl()?

ton*_*ung 7 selenium-firefoxdriver selenium-webdriver

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

另一个例子是

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

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

Ale*_*lor 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());
Run Code Online (Sandbox Code Playgroud)

如果无需使用getCurrentUrl()就可以计算URL,则可能会使您的代码更具可读性。


Mar*_*rco 0

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

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

[...]

driver.get(baseUrl + "url");
Run Code Online (Sandbox Code Playgroud)

driver.get(String url) 相当于 driver.navigate().to(String url)。