watir webdriver: how to switch to another window open with target

11,024

Solution 1

Since you know that the title of the new window should have certain forms, you should be able to locate it via its title using a regex (regular expression denoted with the "/").

browser.window(:title => /known part of title/).use do
  #Whatever you want to do with the popup
end

Alternatively, if you really do not know anything about the popup, you can get the last window created:

browser.windows.last.use do
  #Whatever you want to do with the popup
end

Solution 2

There's a link to the watir-webdriver window switching spec on http://watirwebdriver.com/browser-popups/. You should be able to find the window by :title, :url, or :index. Examples from the spec:

browser.window(:index => 1).use
browser.window(:url => /closeable\.html/).use
browser.window(:title => "closeable window").use
Share:
11,024
earlyadopter
Author by

earlyadopter

Updated on June 09, 2022

Comments

  • earlyadopter
    earlyadopter almost 2 years

    I'm using watir-webdriver, and when clicking on a link with target="_blank" it opens a new window, which I have no control of, but still need to verify that something other than 404 page opened, and if title of that new window contains some keywords. I don't know upfront what title of that window will be (it's not the same all the time), so this solution doesn't help.

    Are there any known ways how to handle those target="_blank" windows through watir-webdriver?

  • stack1
    stack1 almost 9 years
    first line error - syntax error, unexpected ':', expecting ')' browser.window(index: => 1).use
  • orde
    orde almost 9 years
    index: => 1 isn't valid. Try :index => 1 (or index: 1).
  • Justin Ko
    Justin Ko over 8 years
    @paul, sorry the comment is hard to read without formatting. You should update your newly created question with your specific example (including what errors you are getting). The Watir code is correct. The problem might be that you are also mixing with the Page-Object gem.