Open web in new tab Selenium + Python Open web in new tab Selenium + Python python python

Open web in new tab Selenium + Python


Editor's note: This answer no longer works for new Selenium versions. Refer to this comment.


You can achieve the opening/closing of a tab by the combination of keys COMMAND + T or COMMAND + W (OSX). On other OSs you can use CONTROL + T / CONTROL + W.

In selenium you can emulate such behavior.You will need to create one webdriver and as many tabs as the tests you need.

Here it is the code.

from selenium import webdriverfrom selenium.webdriver.common.keys import Keysdriver = webdriver.Firefox()driver.get("http://www.google.com/")#open tabdriver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') # You can use (Keys.CONTROL + 't') on other OSs# Load a page driver.get('http://stackoverflow.com/')# Make the tests...# close the tab# (Keys.CONTROL + 'w') on other OSs.driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w') driver.close()


browser.execute_script('''window.open("http://bings.com","_blank");''')

Where browser is the webDriver


This is a common code adapted from another examples:

from selenium import webdriverfrom selenium.webdriver.common.keys import Keysdriver = webdriver.Firefox()driver.get("http://www.google.com/")#open tab# ... take the code from the options below# Load a page driver.get('http://bings.com')# Make the tests...# close the tabdriver.quit()

the possible ways were:

  1. Sending <CTRL> + <T> to one element

    #open tabdriver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
  2. Sending <CTRL> + <T> via Action chains

    ActionChains(driver).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()
  3. Execute a javascript snippet

    driver.execute_script('''window.open("http://bings.com","_blank");''')

    In order to achieve this you need to ensure that the preferences browser.link.open_newwindow and browser.link.open_newwindow.restriction are properly set. The default values in the last versions are ok, otherwise you supposedly need:

    fp = webdriver.FirefoxProfile()fp.set_preference("browser.link.open_newwindow", 3)fp.set_preference("browser.link.open_newwindow.restriction", 2)driver = webdriver.Firefox(browser_profile=fp)

    the problem is that those preferences preset to other values and are frozen at least selenium 3.4.0. When you use the profile to set them with the java binding there comes an exception and with the python binding the new values are ignored.

    In Java there is a way to set those preferences without specifying a profile object when talking to geckodriver, but it seem to be not implemented yet in the python binding:

    FirefoxOptions options = new FirefoxOptions().setProfile(fp);options.addPreference("browser.link.open_newwindow", 3);options.addPreference("browser.link.open_newwindow.restriction", 2);FirefoxDriver driver = new FirefoxDriver(options);

The third option did stop working for python in selenium 3.4.0.

The first two options also did seem to stop working in selenium 3.4.0. They do depend on sending CTRL key event to an element. At first glance it seem that is a problem of the CTRL key, but it is failing because of the new multiprocess feature of Firefox. It might be that this new architecture impose new ways of doing that, or maybe is a temporary implementation problem. Anyway we can disable it via:

fp = webdriver.FirefoxProfile()fp.set_preference("browser.tabs.remote.autostart", False)fp.set_preference("browser.tabs.remote.autostart.1", False)fp.set_preference("browser.tabs.remote.autostart.2", False)driver = webdriver.Firefox(browser_profile=fp)

... and then you can use successfully the first way.