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

标签 python selenium selenium-webdriver webdriver selenium-chromedriver

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

我能够通过运行打开第一个窗口

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

我该如何解决这个问题?

最佳答案

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

关于python - 使用未检测到的 chromedriver + selenium, python 打开第二个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68558129/

相关文章:

python - 粘性 cumsum/cumprod numpy

java - Selenium 网络驱动程序 : Variable for different countries

java - 如何使用 java 集合收集表中的特定行 id - webdriver

java - 即使我捕获它,也会抛出 NoSuchElementException 吗?

java - 无法使用 Jmeter-Webdriver 访问方法

python - 构建 python pypi 轮子,通常的噩梦

python - 如何使用 Python 请求发布 JSON 数据?

python - WebDriverException : Message: 'The browser appears to have exited before we could connect. 输出为:错误:未指定显示

python - 区分具有相同名称的 html 表单 SELECT 项目

java - 在自动完成栏中如何单击 div ul li Strong 下的特定值