如何在 selenium webdriver 中获取 url 的最后三个值
How to get last three value of url in selenium webdriver
如何获取 selenium webdriver
中 url 的最后三个值?
例如,我的 url 是 localhost/project/user/login
,我想提取其中的 project/user/login
部分。
我使用 driver.getCurrentUrl();
获取当前的 url。
public class url{
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.manage().window().maximize();
driver.findElement(By.name("q")).sendKeys("selenium");
driver.findElement(By.xpath("//*[@id='sblsbb']/button")).click();
List<WebElement> elements = driver.findElements(By.tagName("a"));
System.out.println(elements.size());
String url=driver.getCurrentUrl();
URL aURL = new URL(url);
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
System.out.println("filename = " + aURL.getFile());
System.out.println("ref = " + aURL.getRef());
}
}
如何获取 selenium webdriver
中 url 的最后三个值?
例如,我的 url 是 localhost/project/user/login
,我想提取其中的 project/user/login
部分。
我使用 driver.getCurrentUrl();
获取当前的 url。
public class url{
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.manage().window().maximize();
driver.findElement(By.name("q")).sendKeys("selenium");
driver.findElement(By.xpath("//*[@id='sblsbb']/button")).click();
List<WebElement> elements = driver.findElements(By.tagName("a"));
System.out.println(elements.size());
String url=driver.getCurrentUrl();
URL aURL = new URL(url);
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
System.out.println("filename = " + aURL.getFile());
System.out.println("ref = " + aURL.getRef());
}
}