如何在selenium webdriver中获取url的最后三个值

时间:2015-04-22 05:39:36

标签: java selenium-webdriver

如何在selenium webdriver中获取网址的最后三个值? 例如,我的网址为localhost/project/user/login,我想提取其project/user/login部分。

我使用driver.getCurrentUrl();获取当前网址。

1 个答案:

答案 0 :(得分:0)

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


        }
}