我想在Selenium的同一个标签页面中打开一个新的URL,但我需要以某种方式编辑URL。你能推荐一些工作选择吗?我正在考虑按键导航,但问题是无法检查URL。
要在同一选项卡上打开和编辑网址:
1)首先,您需要获取当前页面的URL,这样做:
getCurrentUrl命令:
用法:用于在浏览器中获取当前打开的网页的URL。
语法:driver.getCurrentUrl();
返回类型:String(返回当前网页的URL)
示例:
String currentURL = driver.getCurrentUrl();
System.out.println(currentURL);
2)现在编辑url:使用上面的url,现在你可以改变任何想要的东西:例如:
String currentURL = driver.getCurrentUrl();
//Let assume CurrentUrl = {something}/product/id1/
String editURL = currentURL.replace("id1", "id2")
OR
String editURL = currentURL + "/something/";
3)在同一选项卡上打开已编辑的URL。
driver.get(editURL);
OR
driver.navigate().to(editURL);
有一种名为getCurrentUrl
的Selenium方法。像这样使用它,
String currentURL = driver.getCurrentUrl();
String newURL = currentURL + "yourEdit";