gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:39:53 24 4
gpt4 key购买 nike

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

另一个例子是

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

任一示例都会导致浏览器返回地址无效。

最佳答案

您可以使用 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,它可能会使您的代码更具可读性。

关于selenium-webdriver - 如何在 webdriver.Navigate().GotoUrl() 中使用相对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16575821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com