Selenium Java to get domain name, URL, and page title using JavascriptExecutor
KW.java
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

class KW
{
    public static void main(String[] args) 
    {   
        System.setProperty("webdriver.chrome.driver", "/home/kodingwindow/drivers/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://kodingwindow.com/testapp/");
        
        JavascriptExecutor jse = (JavascriptExecutor)driver;
        String domain = jse.executeScript("return document.domain;").toString();
        System.out.println("Domain Name - "+domain);
        
        String url = jse.executeScript("return document.URL;").toString();
        System.out.println("URL - "+url);
        
        String title = jse.executeScript("return document.title;").toString();
        System.out.println("Page Title - "+title);
        
        //driver.close();
    }
}
Output
Domain Name - kodingwindow.com
URL - https://kodingwindow.com/testapp/
Page Title - Kodingwindow's TestApp
Advertisement