Search code examples
pythonseleniumfirefoxgeckodriver

Can't open more than max number of tabs with Selenium Webdriver?


I am trying to open a list of different URLs, opening one per tab, but when the number exceeds 20 ~ 21, stop opening tabs.

I've tried to separate the list into groups of 20, and creating new instances of the webdriver, and that works fine, but I would like to know if it's a way to enable more number of tabs using the same instance?

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get('https://stackoverflow.com/')

for i in range(30):
    driver.execute_script("window.open('');")

print(len(driver.window_handles))
time.sleep(3)
driver.quit()

I was trying, to open 30 tabs at once but only opens 21. I'm using python 3.5.0, Firefox 68.0.2 & geckodriver 0.24.0


Solution

  • If you look at the stackoverflow tab, you should see a yellow bar saying the rest has been blocked by the pop-up blocker. (This happens because execute_script runs the script in the context of the web page.)

    To override, set dom.popup_maximum preference to a larger value:

    opts = webdriver.FirefoxOptions()
    opts.set_preference("dom.popup_maximum", 50)
    driver = webdriver.Firefox(options=opts)