Avoiding pop-up blocking when opening multiple Firefox tabs simultaneously in Selenium

Avoiding pop-up blocking when opening multiple Firefox tabs simultaneously in Selenium
Page content

In previous article Set up the Selenium Webdriver. Let’s automate with Python! , I introduced the procedure to build Selenium execution environment in Python.

In this article, I would like to introduce a problem I actually ran into when opening multiple URLs in different tabs at the same time in Firefox and how to deal with it.

What I originally wanted to do

  • Open multiple URLs (about 100) at the same time and check the displayed screen visually.
  • Then, in Selenium, open the URL by executing the window.open() function of Javascript.

Issue: Pop-up blocks appear when opening multiple tabs in Firefox

Run the following code using Firefox WebDriver.

1from selenium import webdriver
2
3driver = webdriver.Firefox()
4driver.get('https://www.google.com/')
5for i in range(0, 100):
6    driver.execute_script(f"window.open('https://www.google.com/', '{i}')")

Then, after about 20 tabs were opened smoothly, the following pop-up block was suddenly displayed.

popup_block

Since popup blocks are only displayed for tabs running the window.open() function, you will definitely be overlooked if a lot of browsers’ tabs are opened. Allowing pop-up blockers will display the page correctly, but it is not automated because of human intervention.

Solution: set the maximum number of pop-ups

Programmatically set the values of the browser’s “configuration” options, which are used by the Options class when creating the Firefox WebDriver. The following sample code uses two properties.

  • dom.disable_open_during_load: Perform a pop-up block during page loading.
  • dom.popup_maximum: the maximum number of separate tabs to be displayed by popups (default: 20).
 1from selenium import webdriver
 2from selenium.webdriver.firefox.options import Options
 3
 4options = Options()
 5options.set_preference("dom.disable_open_during_load", False)
 6options.set_preference('dom.popup_maximum', -1)
 7
 8driver = webdriver.Firefox(options=options)
 9driver.get('https://www.google.com/')
10for i in range(0, 100):
11    driver.execute_script(f"window.open('https://www.google.com/', '{i}')")

When dom.popup_maximum is set to -1, the maximum display limit of tabs is unlimited and popups are not blocked.

References