Luis Valdez
Luis Valdez

Reputation: 107

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

Upvotes: 6

Views: 1791

Answers (2)

Henrik
Henrik

Reputation: 259

Please don't make use of "window.open()" to get new tabs or windows opened. Instead use the new WebDriver New Window API, which all the latest versions of the official Selenium bindings have been already integrated. Note, it's not part of all the drivers yet, but for recent Firefox releases it works.

Given that you are using the Python bindings the following can be used:

driver.switch_to.new_window('tab')

By using this approach there shouldn't be a limitation for opening a lot of tabs.

Upvotes: 2

Nickolay
Nickolay

Reputation: 32073

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)

Upvotes: 4

Related Questions