运行多个 selenium 实例会产生连接错误

问题描述 投票:0回答:1

我正在使用 python3 在 Linux 和 unDetected_chrome 上运行 selenium 自动化脚本。
我也在使用代理。
当我只运行一个实例时,它工作正常并完成工作,但是启动第二个实例时我收到此错误:

2024-04-30 00:09:28 - ERROR - HTTPConnectionPool(host='localhost',
port=44073): Max retries exceeded with url:
/session/2488e939aa54121cb42b165948495489/actions (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x7debd7c86fb0>: Failed to establish a new connection: [Errno 111]
Connection refused'))

尽管每个驱动程序的代理都会发生变化。

我已经尝试了各种我能想到的解决方案。

  • 我已经转移到不同的脚本并从不同的终端运行它们。
  • 我启动了一个带代理的实例和一个不带代理的实例。
  • 我已经对 scrapeme 网站启动了自动化,以查看该网站是否阻止了我,但同样的事情发生了。
    这就是我能想到的一切,但我没有想到任何地方。 我真的很困惑,我不知道错误到底在哪里。
    你能帮我解决这个问题吗?
python selenium-webdriver webautomation
1个回答
0
投票

此解决方案使用 SeleniumBase UC 模式 而不是

undetected-chromedriver
来隐秘地同时启动多个 Web 浏览器:(
pip install seleniumbase
)

import sys
from concurrent.futures import ThreadPoolExecutor
from seleniumbase import Driver
sys.argv.append("-n")  # Activate SB thread-locking as needed

def launch_driver(url):
    driver = Driver(uc=True)
    try:
        driver.uc_open_with_reconnect(url, 4)
        driver.sleep(2)
    finally:
        driver.quit()

urls = ['https://gitlab.com/users/sign_in' for i in range(3)]
with ThreadPoolExecutor(max_workers=len(urls)) as executor:
    for url in urls:
        executor.submit(launch_driver, url)

请务必将示例 URL(使用机器人检测软件)替换为您想要的 URL。

© www.soinside.com 2019 - 2024. All rights reserved.