Posted about 5 years ago. Visible to the public.

How to open a new tab with Selenium

Until recently, you could open a new tab via window.open when using execute_script in Selenium tests. It no longer works in Chrome (will show a "popup 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.

Here is an approach that will insert a link into the page, and have Selenium click it:

Copy
path = "/your/path/here" id = "helper_#{SecureRandom.hex(8)}" execute_script <<-JAVASCRIPT var $helper = $('<a>').attr({ href: #{path.to_json}, id: #{id.to_json}, target: '_blank' }); $helper.prependTo('body').text('click me').css({ zIndex: 9999, position: 'absolute' }); setTimeout(function() { $helper.remove() }, 1000); JAVASCRIPT find("##{id}").click

Note that the link tag is removed after 1 second. You may or may not want to keep this.

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
about 5 years ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more