使用未检测到的chromedriver+selenium、python打开第二个窗口

2024-06-16 11:00:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图打开两个或多个单独的窗口

我可以通过跑步打开第一个窗口

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))

现在我试图通过简单地重复最后一行(drivers.append(uc.Chrome(options=options)))来打开第二个窗口,但它返回了

RuntimeError: you cannot reuse the ChromeOptions object

所以我试过了

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))

这次它回来了

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

我怎样才能解决这个问题


Tags: fromimportadddatadirchromeargumentusers
1条回答
网友
1楼 · 发布于 2024-06-16 11:00:26

这对我有用,我不能使用v2,但它在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)

相关问题 更多 >