简体   繁体   中英

How to get last three value of url in selenium webdriver

How to get last three values of an url in selenium webdriver ? For example, my url is localhost/project/user/login and I want to extract the project/user/login part of it.

I use driver.getCurrentUrl(); to get my current 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());  


        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM