Selenium and Window Handles
An often misunderstood aspect of writing automated browser tests with a Selenium based framework is how Selenium handles tabs, or as Selenium refers to them ‘window handles’. You might imagine that if your test does something that opens a new tab, Selenium will automatically become aware of that and you can interact with the new tab straight away. Unfortunately this is not the case.
Selenium does not have any way of knowing that your browsers state has changed unless you specifically tell it. To understand how this works, you need to be aware that there is an underlying data structure that the browser provides called windowHandles
. This is just an array of IDs that refer to the different tabs that are currently open. By default when the browser is opened, their is just one element in here and the currentWindowHandle
points to this.
Now what happens if your test does something that opens another tab? The windowHandles
structure contains two IDs, one for each tab, but the currentWindowHandle
is still pointing at the initial tab. To interact in any way with the content of the other tab, you need to use whichever Selenium framework you are using to change the current window handle to the correct one. Different frameworks do this in different ways, so look up the correct documentation.
What happens then when a tab gets closed? The tab will no longer be in the windowHandles
but the currentWindowHandle
will still be referencing a window that no longer exists. For this reason you’ll again need to switch to the correct window handle using your test framework.
Note that the tab can get closed by using Selenium to request it to close, or by some browser action.
One final to thing to bear in mind is that there is no guarantee of the order of the handles. You could have two tabs open and the ‘leftmost’ tab in the screen might be the second element, and vice versa.
This pretty much covers all of the pitfalls that you can encounter when working with windows/tabs in Selenium.