Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to open a new tab with Selenium

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 with 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 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:

path = "/your/path/here"
id = "helper_#{SecureRandom.hex(8)}"
execute_script var $helper = $('').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() }, 500);

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.

Happy testing!

If You enjoyed this article , consider other helper articles :

jQuery scroll to element

Ruby Capybara select random value from dropdown

Introducing the Tellurium Selenium Automated Testing Framework

The post How to open a new tab with Selenium appeared first on TestingRepository.

This post first appeared on Testing Repository - Creating Testing, please read the originial post: here

Share the post

How to open a new tab with Selenium


Subscribe to Testing Repository - Creating Testing

Get updates delivered right to your inbox!

Thank you for your subscription