hmje
hmje

Reputation: 117

opening the second window using undetected chromedriver + selenium, python

I'm trying to open two or more separate windows.

I was able to open the first window by running

from selenium import webdriver
import undetected_chromedriver.v2 as uc

options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\username\AppData\Local\Google\Chrome\User Data")

drivers = list()
drivers.append(uc.Chrome(options=options))

Now I tried to open the second window by simply repeating the last line (drivers.append(uc.Chrome(options=options))), but it returned

RuntimeError: you cannot reuse the ChromeOptions object

So I tried

options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\username\AppData\Local\Google\Chrome\User Data")

drivers.append(uc.Chrome(options=options))

This time it returned

WebDriverException: unknown error: cannot connect to chrome at 127.0.0.1:54208
from chrome not reachable

How can I fix this?

Upvotes: 5

Views: 4791

Answers (1)

Ajay Pyatha
Ajay Pyatha

Reputation: 164

This worked for me , I couldn't use v2 but it works in v1.

    import undetected_chromedriver as uc  
    uc.install(executable_path=PATH,)
    drivers_dict={}   
    def scraping_function(link):
            try:
                thread_name= threading.current_thread().name
                    #sometime we are going to have different thread name in each iteration so a little regex might help
                thread_name = re.sub("ThreadPoolExecutor-(\d*)_(\d*)", r"ThreadPoolExecutor-0_\2", thread_name)
                print(f"re.sub -> {thread_name}")
                driver = drivers_dict[thread_name]
            except KeyError:
                drivers_dict[threading.current_thread().name] = uc.Chrome(options=options,executable_path=PATH)
                driver = drivers_dict[threading.current_thread().name]
            driver.get(link)

Upvotes: 2

Related Questions