Selenium get url using getCurrentUrl()

Get url in selenium using getCurrentUrl() Method

Sometimes you need to get the current url during selenium test. You can get the URL of current open page using getCurrentUrl() method in selenium.
  • getCurrentUrl() method is part of WebDriver interface which extends SearchContext interface.
  • It will fetch the url from current open page in browser.
  • It will return rul as a string.
  • Syntax to use getCurrentUrl() : driver.getCurrentUrl().
Below given example will show you how to get url of current open page using getCurrentUrl() method in selenium.
Selenium getCurrentUrl() Method

Selenium getCurrentUrl() method example

package testPackage;

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

public class getCurrentUrlExample {

	public static void main(String[] args) {
				
		System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		
		driver.navigate().to("http://only-testing-blog.blogspot.com/2013/09/testing.html");
		
		//Get current url of page and print it in console.
		String url = driver.getCurrentUrl();
		System.out.println("Current URL is : "+url);
	}
}
In above given example, getCurrentUrl() method will fetch url string of page and print it in console.

No comments:

Post a Comment