【问题标题】:opening the second window using undetected chromedriver + selenium, python使用未检测到的 chromedriver + selenium,python 打开第二个窗口
【发布时间】:2021-10-04 01:27:46
【问题描述】:

我正在尝试打开两个或更多单独的窗口。

我可以通过运行打开第一个窗口

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

我该如何解决这个问题?

【问题讨论】:

  • 您找到解决方案了吗?
  • 很遗憾没有
  • 我只是用 undetected-chromedriver v1 做的,看看,让我知道它是否有效

标签: python selenium selenium-webdriver webdriver selenium-chromedriver


【解决方案1】:

这对我有用,我无法使用 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)

【讨论】:

    最近更新 更多