How can I use selenium’s “getCurrentUrl” method to capture the current URL of the web page programmatically?

301    Asked by DeepakMistry in QA Testing , Asked on Apr 4, 2024

I am currently automating a web-based application by using Selenium WebDriver. When I was navigating through different pages, I needed to capture the current URL of the web page programmatically. How can I use the selenium’s “getCurrentUrl” method for achieving this particular Objective? 

Answered by Deepa bhawana

 In the context of selenium, here is how you can use selenium’s WebDriver “getCurrentUrl” method in Java programming language for capturing the current URL of a web-based page during the automation:-

Using “getCurrentUrl” method
In the Java programming language, you can use the “getCurrentUrl” method provided by the Selenium WebDriver’s “WebDriver” interface to retrieve the current URL of the web-based page.
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.chrome.ChromeDriver;
Public class GetCurrentURLExample {
    Public static void main(String[] args) {
        // Set up ChromeDriver path
        System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”);
        // Initialize ChromeDriver
        WebDriver driver = new ChromeDriver();
        // Navigate to a web page
        Driver.get(https://www.example.com);
        // Get the current URL of the web page
        String currentUrl = driver.getCurrentUrl();
        System.out.println(“Current URL: “ + currentUrl);
        // Close the browser
        Driver.quit();
    }
}

Your Answer

Interviews

Parent Categories