How to get the current URL on a page in Selenium Java

asked 2 years ago

To get the current URL of the webpage you are testing, you can use the getCurrentUrl() method provided by Selenium WebDriver. 

Example:

driver.getCurrentUrl();
System.out.println(driver.getCurrentUrl());


Example code in Selenium Java

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrowserSetup {
public static void main(String[] args) {
// Set the system property to tell Selenium where the ChromeDriver executable is located
// Selenium 4.6.0 or later, Selenium Manager automatically manages the browser drivers.
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
// Create a new instance of Chrome browser using ChromeDriver
WebDriver driver = new ChromeDriver();
// Navigate to the specified URL in the browser
driver.get("https://www.qafeast.com/");
// Print the current URL loaded in the browser to the console
System.out.println("Test One: " + driver.getCurrentUrl());
// Close the browser and safely end the WebDriver session
driver.quit();
}
}


The Output is 

Test One: https://www.qafeast.com/