Home > Software design >  How to Open 20 window in chrome using webDriver (python)
How to Open 20 window in chrome using webDriver (python)

Time:04-05

I want to achieve to open 20 or 10 tabs at all the same time with one driver.. and if i use an for loop.. it just open but closes the exsiting and idk why i think i have commited some mistakes.. and i want to know the things.. 1. Open Differnt Windows 2. Shouldnt Crash 3. Need to enter diff diff values

My Code :

def start():
        options = webdriver.ChromeOptions()
        options.add_argument(
            "user-data-dir=C:\\Users\\rohit\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
        options.add_experimental_option("useAutomationExtension", False)
        options.add_experimental_option(
            "excludeSwitches", ["enable-automation"])
        options.add_experimental_option('useAutomationExtension', False)
        options.add_argument("--disable-blink-features=AutomationControlled")
        # options.add_argument('--no-sandbox')
        options.add_argument('start-maximized')
        options.add_argument('--disable-infobars')
        # options.add_argument('--disable-dev-shm-usage')
        # options.add_argument('--disable-browser-side-navigation')
        options.add_argument("--remote-debugging-port=9222")
        options.add_argument('--disable-gpu')
        # options.add_argument("--disable-logging")
        options.add_argument("--log-level=3")
        #options.headless = True
        # to open 20 diff windows
        for i in range(20):
            driver = webdriver.Chrome()
                #executable_path=r'C:\Users\rohit\AppData\Local\Programs\Python\Python39\Scripts\chromedriver.exe', options=options)
            with open('file.txt') as k:
                accs = k.readlines()
                for line in accs:
                    unam, pwd, a, b,c,d,e,f,g,h,i,j,k,l = line.split(':')
                
        print('Vera Started')
        #driver = webdriver.Chrome(options=options)
        print('Driver Loaded')
        # unam = input('Username: ')
        # pwd = input('Password: ')
        scraper.login(driver, unam, pwd)
        ask = input('Enter 1 to continue: ')
        if ask == '1':
            # a = input('Enter First Name: ')
            # b = input('Enter Last Name: ')
            # c = input('Enter Middle Name: ')
            # d = input('Enter Suffix: ')
            # e = input('Enter Occupation: ')
            # f = input('Enter SSN: ')
            # g = input('Enter DOB: ')
            # h = input('Enter Steert Adress: ')
            # i = input('Enter Apartmenet NO: ')
            # j = input('Enter City: ')
            # k = input('Enter State: ')
            # l = input('Enter Zip: ')
            scraper.taxpayer(driver, a, b, c, d, e, f, g, h, i, j, k, l)

I hope i am right and i wish there is no Error Also Even though i have setup log level 3 it still shows error need a fix for this Thank you Kindy answer me real quick

CodePudding user response:

Using many times driver = webdriver.Chrome() you try to open many browsers, not tabs.

You would have to assign every browser to separated variable because when you assign again to the same variable then it automatically removes previous browser.

all_drivers = []

for i in range(20):
    driver = webdriver.Chrome()

    all_drivers.append(driver)

    driver.get("https://books.toscrape.com/")

Besides using list you have access to all browsers - all_drivers[0],all_drivers[1], etc.

Using single driver you could access only last open browser.


BTW: when I run with Firefox() then it keeps open all browsers even if I assign new browser to the same variable - but this way I can't access previous browsers.


And if you want to open tabs then you may do

driver.switch_to.new_window('tab')
driver.get("https://books.toscrape.com/")

or use JavaScript

driver.execute_script('window.open("https://books.toscrape.com/","_blank");')

To switch between tabs you can get handler to current tab

tab_id = driver.current_window_handle

and later use it to switch to this tab

driver.switch_to.window(tab_id)

For more tabs you could keep them on list.


Full working code:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
#from webdriver_manager.firefox import GeckoDriverManager
import time

driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
#driver = webdriver.Firefox(executable_path=ChromeDriverManager().install())

time.sleep(2)

driver.switch_to.new_window('tab')
driver.get("https://books.toscrape.com/")
tab1_id = driver.current_window_handle

time.sleep(2)

driver.switch_to.new_window('tab')
driver.get("https://quotes.toscrape.com/")
tab2_id = driver.current_window_handle

time.sleep(2)

driver.switch_to.window(tab1_id)

time.sleep(2)

driver.switch_to.window(tab2_id)

time.sleep(2)

driver.switch_to.window(tab1_id)
  • Related