带有 ChromeDriver 和 Chrome 的 Selenium 4.11.2

Selenium 4.11.2 with ChromeDriver and Chrome

提问人:Paulvanderheijden 提问时间:8/7/2023 最后编辑:undetected SeleniumPaulvanderheijden 更新时间:8/9/2023 访问量:5079

问:

我正在尝试运行这个简单的代码..但是我收到一个无法修复的错误。 有人可以帮我吗?

Chrome驱动程序已安装,我检查:

pi@Rpi:~ $ chromedriver --version
ChromeDriver 92.0.4515.98 (564abd8de2c05f45308eec14f9110a10aff40ad9-refs/branch-heads/4515@{#1501})

法典:

from selenium import webdriver

driver=webdriver.Chrome()
driver.get("https://www.google.com/")

错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/common/selenium_manager.py", line 123, in run
    completed_proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "/usr/lib/python3.7/subprocess.py", line 472, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib/python3.7/subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.7/subprocess.py", line 1522, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/usr/local/lib/python3.7/dist-packages/selenium/webdriver/common/linux/selenium-manager'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/common/driver_finder.py", line 38, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/common/selenium_manager.py", line 90, in driver_location
    output = self.run(args)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/common/selenium_manager.py", line 129, in run
    raise WebDriverException(f"Unsuccessful command executed: {command}") from err
selenium.common.exceptions.WebDriverException: Message: Unsuccessful command executed: /usr/local/lib/python3.7/dist-packages/selenium/webdriver/common/linux/selenium-manager --browser chrome --output json


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/pi/src/test.py", line 3, in <module>
    driver=webdriver.Chrome()
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 50, in __init__
    keep_alive,
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/chromium/webdriver.py", line 51, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/common/driver_finder.py", line 41, in get_path
    raise NoSuchDriverException(msg) from err
selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain driver for chrome using Selenium Manager.; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location

我尝试重新安装 selenium 几次。我还尝试重新安装网络驱动程序。已尝试查找路径。但我读到它已经没有必要了。我将 Chromedriver 放在与 Python 项目相同的目录中

项目快照 + Chromedriver:

项目地点 + Chromedrive

python google-chrome selenium-chromedriver seleniummanager firefox microsoft-edge selenium4

评论

0赞 Codist 8/7/2023
在 Python 中,您需要使用 Service 类来指示 chromedriver 的安装位置。然后,将对 Service 类的引用传递给 Chrome 构造函数。请务必注意,chromedriver 的版本必须与您使用的 Chrome 浏览器版本一致

答:

0赞 Micraow 8/7/2023 #1

您的计算机上是否安装了Chrome(我的意思是带有GUI的Chrome)?chromedriver 只是一个驱动程序。请安装 Chrome 浏览器和相应版本的 chormedriver。

评论

0赞 Paulvanderheijden 8/7/2023
是的,我不是头。所以我可以从“应用”菜单中打开 Chromium
0赞 Paulvanderheijden 8/7/2023
如果我在端口 9515 上运行 THIS: /usr/bin/chromedriver 结果:pi@Rpi:~ $ /usr/bin/chromedriver 启动 ChromeDriver 92.0.4515.98 (564abd8de2c05f45308eec14f9110a10aff40ad9-refs/branch-heads/4515@{#1501}) 只允许本地连接。请参阅 chromedriver.chromium.org/security-considerations,了解有关确保 ChromeDriver 安全的建议。ChromeDriver 已成功启动。所以我认为安装很好。
0赞 limosumi 8/7/2023 #2

1.确保Webdriver的版本与Web版本一致或最接近。 2.将 Webdriver 放在 python 根目录下

评论

0赞 Paulvanderheijden 8/7/2023
您的意思是项目根或 Python 3.7 安装根?
0赞 Paulvanderheijden 8/7/2023
Chromiun Web 浏览器是: Versie 92.0.4515.98 (Officiële build) Built on Raspbian ,运行在 Raspbian 10(32 位)chromium-chromedriver (92.0.4515.98~buster-rpt2) 上。
0赞 Paulvanderheijden 8/7/2023
/lib/python3.7 把 chromedriver 放在这里?
0赞 limosumi 8/8/2023
from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService service = ChromeService(executable_path=“your_dir_path/chromedriver.exe_”) driver = webdriver.Chrome(service=service) driver.get(“url”)
1赞 undetected Selenium 8/8/2023 #3

我将 Chromedriver 放在与 Python 项目相同的目录中

当您使用 Selenium v4.11.2 时,您无需明确下载 ChromeDriverGeckoDriver 或任何浏览器驱动程序,甚至不需要再使用webdriver_manager。您只需要确保安装了所需的浏览器客户端,即


Selenium 管理器

Selenium Manager 是与 集成的新工具,它将有助于获得一个开箱即用的工作环境来运行 Selenium。Selenium Manager 的 Beta 1 将为 Chrome、Firefox、Edge 等浏览器客户端配置浏览器驱动程序,如果它们不在 PATH 上。


溶液

作为解决方案,您可以简单地执行以下操作:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.google.com/")

TL的;博士

Chrome for Testing:用于浏览器自动化的可靠下载