How to open a new tab with Selenium
Many of our tests can require to open a new tab and perform some further operations. Seems pretty straight forward , it used to work using
execute_script, well I’m sorry to disappoint you guys but is no longer working in Chrome , will basically show a pop-up with blocked notification.
This is because browsers usually block
window.open unless the user interacted with an element for security reasons. I am not sure why it did work via Selenium before.
The approach is basically to insert a link into the page and have selenium to click on it:
As you can see the link will get removed in 500 milliseconds so is not going to affect your tests at all.
You might wander if the link gets removed even if the code above that will fail , well , on a page reload yes the link will get removed anyway , so if you start a new scenario with a brand new session the link will not be chunked into your test page.
If You enjoyed this article , consider other helper articles :