返回

解决`undetected-chromedriver`中的`TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path'`错误

python

解决 undetected-chromedriverTypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path' 错误

引言

undetected-chromedriver 是一个流行的 Chrome 驱动程序,允许开发者在自动化测试中绕过检测。然而,在使用它时,你可能会遇到 TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path' 错误。这个错误表明 executable_path 参数没有正确指定。

错误原因

undetected-chromedriver 需要 executable_path 参数来指定 Chrome 可执行文件的位置。如果不提供此参数,驱动程序将无法启动浏览器,从而导致该错误。

解决方案

解决此错误的步骤很简单:

  1. 确定 Chrome 可执行文件的路径。
  2. undetected-chromedriver 初始化代码中指定 executable_path 参数,指向该路径。

例如:

import undetected_chromedriver as uc

# 指定 Chrome 可执行文件的路径
chrome_path = r"C:\path\to\chrome.exe"

# 使用 `executable_path` 参数初始化驱动程序
driver = uc.Chrome(executable_path=chrome_path)

# 访问 Google 主页
driver.get("https://www.google.ru/")

其他注意事项

在指定 executable_path 时,请确保:

  • 路径是正确的并且指向有效的 Chrome 可执行文件。
  • Chrome 版本与 undetected-chromedriver 版本兼容。
  • 你拥有在指定目录下运行 Chrome 可执行文件的权限。

结论

通过遵循上述步骤,你可以解决 undetected-chromedriver 中的 TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path' 错误,并成功启动浏览器以进行自动化测试。

常见问题解答

1. 为什么我需要指定 executable_path

undetected-chromedriver 需要知道 Chrome 可执行文件的位置才能启动浏览器。

2. 如何查找 Chrome 可执行文件的路径?

在 Windows 上,它通常位于 C:\Program Files (x86)\Google\Chrome\Application\chrome.exe。在 Mac 上,它位于 /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

3. 如果我仍然收到该错误怎么办?

  • 检查 Chrome 版本与 undetected-chromedriver 版本是否兼容。
  • 确保你有权在指定目录下运行 Chrome 可执行文件。
  • 尝试重新安装 undetected-chromedriver

4. 我可以在 Linux 上使用 undetected-chromedriver 吗?

是的,undetected-chromedriver 支持 Linux。但是,你可能需要根据你的 Linux 发行版进行额外的配置。

5. undetected-chromedriverchromedriver 有什么区别?

undetected-chromedriver 专门设计用于绕过自动化检测,而 chromedriver 是一个标准的 Chrome 驱动程序,没有此功能。