0

I'm using Selenium Webdriver + Java + TestNG (in case that matters to anything)

What I'm trying to do is get the current domain, store it as a variable, then append something onto that variable and navigate to the resulting URL.

This is what I'm trying and it gets the domain but fails on the navigation. Says it's an invalid argument.

String CurrentURL = driver.getCurrentUrl();
JavascriptExecutor js = (JavascriptExecutor) driver;
String domain = (String) js.executeScript("return document.domain");
System.out.println("My Current Domain is: "+domain);
driver.navigate().to(domain+"/lightning/o/Lead/home");

2 Answers2

0

document.domain doesn't return http/https, e.g. it returns "stackoverflow.com" for this site.

I would use what you started with,

String CurrentURL = driver.getCurrentUrl();

turn that URL into a java.net.URI and then use .getHost(). You can wrap this all into a method like

public static String getDomainName(String url) throws URISyntaxException {
    URI uri = new URI(url);
    String domain = uri.getHost();
    return domain.startsWith("www.") ? domain.substring(4) : domain;
}

and call it like

String domainName = getDomainName(driver.getCurrentUrl());

This answer is where I got the method and it lists caveats around this approach but it sounds like it might work for you.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Thank you! But, it's getting what I need for the domain. I see that when I println. The issue is that I can't make it navigate to that domain (and append to it) and I don't understand why. It's the navigate part that I'm trying to make work, it doesn't make sense to me why it's not working but then again, I'm learning java as I go – Shelley Collett Sep 17 '19 at 16:59
0

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");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Okay, this worked but it required that I put throws URISyntaxException in my function that calls it. Since this is a navigation thing, it's going to be called in every single script. Is that throws URISyntaxException going to cause any problems? I'm not familiar with having to use something like that since I'm a relative newbie. – Shelley Collett Sep 17 '19 at 17:16
  • @ShelleyCollett Yes, you are right. _URI_ should throw **URISyntaxException**. Updated the answer. I use the base _Exception_ for development purpose within my framework. – undetected Selenium Sep 17 '19 at 19:33