我有以下代码,可以在当前安装 chrome 122 的计算机上正常运行 -
import undetected_chromedriver as uc
driver = uc.Chrome()
driver.get('https://ballzy.eu/en/men/sport/shoes')
但是当我在安装了不同 chrome 版本(例如 120)的计算机上运行此代码时,我收到以下错误 -
(selenium) C:\DEV\Fiverr\ORDER\stefamn_jan669_jankore_janxx2\Ballzy>python test3.py
Traceback (most recent call last):
File "C:\DEV\Fiverr\ORDER\stefamn_jan669_jankore_janxx2\Ballzy\test3.py", line 2, in <module>
driver = uc.Chrome(version_main=122)
File "C:\DEV\.venv\selenium\lib\site-packages\undetected_chromedriver\__init__.py", line 466, in __init__
super(Chrome, self).__init__(
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
super().__init__(
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 61, in __init__
super().__init__(command_executor=executor, options=options)
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 208, in __init__
self.start_session(capabilities)
File "C:\DEV\.venv\selenium\lib\site-packages\undetected_chromedriver\__init__.py", line 724, in start_session
super(selenium.webdriver.chrome.webdriver.WebDriver, self).start_session(
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 292, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
self.error_handler.check_response(response)
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:50596
from session not created: This version of ChromeDriver only supports Chrome version 122
Current browser version is 120.0.6099.200
是否有可能自动下载正确的 chromedriver?
(当我使用普通的硒驱动程序时 - 我只使用以下驱动程序定义,它在多台计算机上运行良好)
srv=Service()
driver = webdriver.Chrome (service=srv, options=options)
我该如何使用未检测到的 chromedriver 来执行此操作,以便它可以在不同计算机上的不同 chrome 版本安装上运行?
这是一种方法:
pip install selenium chromedriver_autoinstaller
代码中:
import chromedriver_autoinstaller as chromedriver
chromedriver.install()
dr = webdriver.Chrome()