Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 247
Luis Valdez Avatar asked Aug 21 '19 23:08

Luis Valdez


People also ask

Can you open multiple tabs in Selenium?

Just like you might open web pages in different tabs locally, it's also possible to have multiple tabs up in one browser during a Selenium test.

How do I open multiple browsers in Selenium Webdriver?

Create an XML which will help us in parameterizing the browser name and don't forget to mention parallel="tests" in order to execute in all the browsers simultaneously. Execute the script by performing right-click on the XML file and select 'Run As' >> 'TestNG' Suite as shown below.


2 Answers

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)
like image 198
Nickolay Avatar answered Oct 16 '22 01:10

Nickolay


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.

like image 31
Henrik Avatar answered Oct 16 '22 02:10

Henrik