As per the java docs of Class URI:
public final class URI
extends Object
implements Comparable<URI>, Serializable
Represents a Uniform Resource Identifier (URI) reference.
The URI instance has the following nine components:
Component Type
-------------------- ------
scheme String
scheme-specific-part String
authority String
user-info String
host String
port int
path String
query String
fragment String
As you have already extracted the current url through getCurrentUrl()
you can convert the current url to uri and extract the each and every component as follows (using https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/package-summary.html
):
Code Block:
import java.net.URI;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class A_demo
{
public static void main(String[] args) throws URISyntaxException
{
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/package-summary.html");
String CurrentURL = driver.getCurrentUrl();
URI uri = new URI(CurrentURL);
System.out.println("Scheme: "+uri.getScheme());
System.out.println("Scheme-specific-part: "+uri.getSchemeSpecificPart());
System.out.println("Authority: "+uri.getAuthority());
System.out.println("User-info: "+uri.getUserInfo());
System.out.println("Host: "+uri.getHost());
System.out.println("Port: "+uri.getPort());
System.out.println("Path: "+uri.getScheme());
System.out.println("Query: "+uri.getQuery());
System.out.println("Fragment: "+uri.getFragment());
}
}
Console Output
Scheme: https
Scheme-specific-part: //seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/package-summary.html
Authority: seleniumhq.github.io
User-info: null
Host: seleniumhq.github.io
Port: -1
Path: https
Query: null
Fragment: null
Finally, to construct the resultant URL of https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.html
you can do:
driver.navigate().to(uri.getScheme()+"://"+uri.getHost()+"/selenium/docs/api/java/org/openqa/selenium/WebDriver.html");