Home > database >  Python Selenium - open multiple tabs in loop
Python Selenium - open multiple tabs in loop

Time:09-21

Im trying to make a python script using selenium that will open multiple chrome tabs. So I made a loop like below but it stops on 9 windows. Do you have any ideas what can be wrong here?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
number = 0
while number < 100:
  driver.execute_script('''window.open("url","_blank");''')
  driver.switch_to.window(driver.window_handles[number])
  number = number   1

CodePudding user response:

I tried such code and it worked properly. I did not count the opened tabs, but they were definitely much more than 10 and the program finished with no errors.
This is the code I used (like your, but with more details):

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = 'https://www.some_rela_url'
driver.get(url)
number = 0
for number in range(100):
  driver.execute_script('''window.open("url","_blank");''')
  driver.switch_to.window(driver.window_handles[number])

CodePudding user response:

Like this it works and opens 100 tabs

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common import window

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
for _ in range(100):
    driver.switch_to.new_window(window.WindowTypes.TAB)
  • Related