In this Selenium tutorial, you will learn how to get the current URL in Selenium Java with real-time coding examples. We will cover the necessary steps and provide practical demonstrations to help you understand and implement this feature effectively.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class GetCurrentURL {

	public static void main(String[] args) throws InterruptedException {
        // Set the path to the ChromeDriver executable
       WebDriverManager.chromedriver().setup();

        // Create an instance of the ChromeDriver
        WebDriver driver = new ChromeDriver();
        
        //Delete all cookies.
        driver.manage().deleteAllCookies();
        
        //Maximize the browser.
        driver.manage().window().maximize();

        // Navigate to a website
        driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
       
        Thread.sleep(3000);

        // Perform further actions or tests...
       // Verify that the title of the page
        String expUrl = "https://opensource-demo.orangehrmlive.com/web/index.php/auth/login";
        String actUrl = driver.getCurrentUrl();
        
        if(actUrl.equalsIgnoreCase(expUrl)) {
        	System.out.println("Test Case Pass");
        }else {
        	System.out.println("Testcase failed");
        }
        

        // Close the browser
        driver.close();
    }

}

By Avinash

Leave a Reply

Your email address will not be published. Required fields are marked *