python-3.x undetected_chromedriver需要比自动提供的更新的chromedriver -版本

n3ipq98p  于 2022-11-19  发布在  Python
关注(0)|答案(1)|浏览(1024)

在示例化一个新的undetected-chromedriver示例之前,我确保通过this post中提到的python-chromedriver-autoinstaller库安装了最新的ChromeDriver version

import chromedriver_autoinstaller
chromedriver_autoinstaller.install()

'C:\\Users\\username\\project_path\\lib\\site-packages\\chromedriver_autoinstaller\\106\\chromedriver.exe'

如图所示,它会自动安装版本106,但在错误消息中(由于意图通过UndetectedChromeDriver.Chrome(options=self._browser_options)示例化未检测到的Chrome驱动程序而导致),它指出我需要下一个版本107
selenium.common.exceptions.WebDriverException:消息:未知错误:无法从未创建的会话连接到位于www.example.com的chrome127.0.0.1:...此版本的ChromeDriver仅支持Chrome版本107当前浏览器版本为106.0.5249.119
另一种自动安装所需chromedriver的传统方法也不起作用:

from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
import undetected_chromedriver as UndetectedChromeDriver

return UndetectedChromeDriver.Chrome(
    options=self._browser_options,
    service=ChromeService(ChromeDriverManager().install()))

我需要远程部署这个,所以应该有一种方法可以自动获取并安装所需的ChromeDriver - Version。
包含回溯的完整错误消息如下:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\username\project_path\lib\site-packages\undetected_chromedriver\__init__.py", line 103, in __init__
    def __init__(
  File "C:\Users\username\project_path\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 35, in __init__
    def __init__(self, executable_path=DEFAULT_EXECUTABLE_PATH, port=DEFAULT_PORT,
  File "C:\Users\username\project_path\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 37, in __init__
    def __init__(self, browser_name, vendor_prefix,
  File "C:\Users\username\project_path\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 195, in __init__
    def __init__(self, command_executor='http://127.0.0.1:4444',
  File "C:\Users\username\project_path\lib\site-packages\undetected_chromedriver\__init__.py", line 599, in start_session
    def start_session(self, capabilities=None, browser_profile=None):
  File "C:\Users\username\project_path\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 346, in start_session
    def start_session(self, capabilities: dict, browser_profile=None) -> None:
  File "C:\Users\username\project_path\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 408, in execute
    def execute(self, driver_command: str, params: dict = None) -> dict:
  File "C:\Users\username\project_path\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 102, in check_response
    def check_response(self, response: Dict[str, Any]) -> None:

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:...
from session not created: This version of ChromeDriver only supports Chrome version 107
Current browser version is 106.0.5249.119

Stacktrace:

Backtrace:
    Ordinal0 [0x00FEACD3+2075859]
    Ordinal0 [0x00F7EE61+1633889]
    Ordinal0 [0x00E7B7BD+571325]
    Ordinal0 [0x00E9DFCD+712653]
    Ordinal0 [0x00E96D24+683300]
    Ordinal0 [0x00E96B01+682753]
    Ordinal0 [0x00ECDBD3+908243]
    Ordinal0 [0x00ECD6AA+906922]
    Ordinal0 [0x00EC7136+880950]
    Ordinal0 [0x00E9FEFD+720637]
    Ordinal0 [0x00EA0F3F+724799]
    GetHandleVerifier [0x0129EED2+2769538]
    GetHandleVerifier [0x01290D95+2711877]
    GetHandleVerifier [0x0107A03A+521194]
    GetHandleVerifier [0x01078DA0+516432]
    Ordinal0 [0x00F8682C+1665068]
    Ordinal0 [0x00F8B128+1683752]
    Ordinal0 [0x00F8B215+1683989]
    Ordinal0 [0x00F96484+1729668]
    BaseThreadInitThunk [0x774CFA29+25]
    RtlGetAppContainerNamedObjectPath [0x779E7BBE+286]
    RtlGetAppContainerNamedObjectPath [0x779E7B8E+238]

编辑(有关Chrome版本的更多信息):

C:\Users\username>REG QUERY HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon /v Version

HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon
    Version    REG_SZ    106.0.5249.119
z9smfwbn

z9smfwbn1#

我昨天就遇到了同样的问题!
在花了一些时间寻找解决方案后,我发现了2016年的this thread,这表明google chrome在自动更新时不会更改注册表中的版本。这就解释了为什么KEY_CURRENT_USER\Software\Google\Chrome\BLBeacon的版本比您当前的浏览器版本要旧。
这显然是相当烦人的,因为chromedriver_autoinstaller使用这个确切的值来确定当前的浏览器版本,从而产生这个错误。
我还没有找到任何解决方案,以解决这个确切的问题,但设法找到另一种方式来自动下载最新的正确的chrome驱动程序使用webdriver_manager库(它本质上和chromedriver_autoinstaller做的一样,但是有一些额外的功能,比如可以给予它一个确切的chrome驱动程序版本)!它使用this thread,通过命令提示符获取正确的当前浏览器版本,缺点是它需要一个硬编码的浏览器可执行文件路径:

import re
import subprocess

chrome_version = subprocess.check_output(
    r'wmic datafile where name="C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" get Version /value',
    shell=True
    )
    
# Retrieve the current browser major version
chrome_version = re.sub('Version=', '', chrome_version.decode('utf-8').strip()).split('.')[0]

检索到版本后,我们可以通过Google API手动找到相关的chrome驱动程序版本,然后用它示例化ChromeDriverManager(灵感来自webdriver_manager的内部工作方式):

import urllib
from webdriver_manager.chrome import ChromeDriverManager

latest_release_url = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE"

latest_release_url_version = f"{latest_release_url}_{chrome_version}"

with urllib.request.urlopen(latest_release_url_version) as response:
    chrome_driver_version = response.read().decode("utf-8")

chromedriver_filepath = ChromeDriverManager(version=chrome_driver_version).install()

我希望这个错误有一天会被修复,因为这个解决方案看起来不是很健壮,但它似乎做的工作。而且,我有opened an issuewebdriver_manager的GitHub仓库,可能会做同样的chromedriver_autoinstaller
希望能有所帮助!

相关问题