2015-04-22 52 views
0

如何獲得selenium webdriver一個網址的最後三個值? 例如,我的網址是localhost/project/user/login,我要提取它的project/user/login部分。如何獲得硒URL的最後三個值的webdriver

我用driver.getCurrentUrl();讓我當前的URL。

+0

可能會有幫助:http://stackoverflow.com/questions/4050087/how-to-obtain-the-last-path-segment-of-an-uri – sap1ens

+0

這個問題的辦法是在不編程在webdriver :) –

回答

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


     } 
} 
+0

感謝比賓,它的工作,我得到了所需的URL使用aURL.getPath() – vnnogile