Getting the URL of the current page using Selenium WebDriver.

SeleniumAutomation TestingTesting Tools

We can obtain the URL of the current page with Selenium webdriver. This is achieved with the help of getCurrentUrl() method. It fetches the URL of the opened application. This method accepts no parameters and strings the URL in the form of String.

Syntax −

String strUrl = driver.getCurrentUrl();

Example

Code Implementation.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class CurrentUrl{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // get current URL and print
      String strUrl = driver.getCurrentUrl();
      System.out.println("Current Url is:"+ strUrl);
      driver.quit();
   }
}

Output

raja
Published on 28-Aug-2020 16:05:20
Advertisements