Watir’s @browser.url returning the previous page’s url

I’ve come across a page which, when I navigate to it and ask it for it’s url (using @browser.url) gives me the previous page’s url. Very frustrating. To get around this, you can use direct DOM manipulation. Here’s a call you can use in place of @browser.url which will give you the actual url of the page:

my_current_url = @browser.js_eval("document.location.toString();")

The js_eval call takes a javascript command and returns any result. In this case, we ask the document (the root element of the document object model) and ask it for its location as a string. The javascript is evaluated, and the current url in the browser is returned. BTW, if you look at the firewatir code, the js_eval method is at the core of firewatir – it’s what passes commands to jssh and gets the results back.

3 thoughts on “Watir’s @browser.url returning the previous page’s url

  1. Hi There,
    Did execute this, but am getting the foll error:

    undefined method `js_eval’ for # (NoMethodError)

    For watir 1.6.5 and IE7 on windows Vista

    Is there any library that i need to require before calling javascript functions?

    Regards,
    Pals

  2. I should have mentioned this… js_eval is the method for firewatir; execute_script is the watir equivalent. Sorry!

  3. I have tried it.

    my_current_url = @browser.execute_script(“document.location.toString();”)

    It does not work. You’ll need to add ‘return’

    my_current_url = @browser.js_eval(“return document.location.toString();”)

    And it works :D

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>