jmatheusvaz
jmatheusvaz

Reputation: 25

Get url from window.location with selenium

Does anybody know how can i get a url between a javascript window.location.href="url"; using seleniumhq web-driver in java.

Imagine a flow like this.

Link Page > Page 2 > Page 3 > Final Page

"Link Page" has the link:

<a href="linkToPage2"> Link </a> and then Selenium clicks the link element with something like this:

webElement.click();

Page 2 executes the window.location.href="Page 3" and then Page 3 send the redirect to the Final Page.

is it possible to get the url from Page 3 or even the history navigation?

Upvotes: 2

Views: 8776

Answers (3)

JohnP2
JohnP2

Reputation: 2197

The easiest way in selenium webdriver (in Java):

String browserUrl = driver.getCurrentUrl();

You can also make javascript calls in selenium:

JavascriptExecutor js = (JavascriptExecutor) driver;
String browserUrl = (String) js.executeScript("return window.top.location.href.toString()");
System.out.println("Your browser URL is " + browserUrl);

Upvotes: 1

some_other_guy
some_other_guy

Reputation: 3404

String url = selenium.getLocation();

System.out.println(url);

Upvotes: 1

JacekM
JacekM

Reputation: 4099

So far I cannot think of any other way than using a proxy that would record all requests made by browser. It is possible to set up such a proxy and control it from your code. You didn't specify the language you use for writing your tests. If it's Java then Browsermob may be helpful. If it's C# take a look at FiddlerCore.

Upvotes: -1

Related Questions