When a new browser window is opened, you need to switch over to it in order to interact with it.
The vast majority of the time, there is only one other window you want to use, so,
as of Watir 6.18, the easiest way to work with that window is with
It can only be used if there are only 2 windows, and all it does is switch to the other one,
no additional locating required. Just don’t forget to switch back when you need to.
If you need to work with more than one window, or want more control than
then you need to be able to specify which window to work with.
There are 3 primary selectors for windows:
:title- typically the easiest
:url- often used with a
Regexpvalue (see example below)
:element- a unique element might be the least brittle (new as of Watir 6.18!)
Locating by index is no longer supported
The WebDriver specification does not require drivers to provide the list of
open windows in any particular order.
Since early versions of Watir, many users have
in their code to indicate “the most recently opened browser.” Since that is not always
the case (especially with both Safari & Internet Explorer). For Watir 7 we’re removing support
:index as a locator as well as indexing from
Most of the time,
Browser#switch_window will be good enough,
and for the times when it isn’t, you’ll need to use one of the other 3 locators.
Once you’ve specified the window you want to use, you need to switch over to it.
This is done with the
#use method and can be accomplished either with or without a block.
When using without a block, you might also use the
method that was introduced in Watir 6.7.
When using a block to interact with the window, Watir will automatically switch back to the window you started on.
A common issue when working with windows is that they take time to load, so synchronization has been notoriously difficult. As of Watir 6.18, all of the power of Watir’s waiting functionality is available to window collections. For example, you can synchronize by waiting for a specific number of windows:
See the Window Switching Spec for more examples of working with windows.