Selenium 4.11.2 with ChromeDriver和Chrome

q35jwt9p  于 2023-09-28  发布在  Go
关注(0)|答案(3)|浏览(225)

我试着运行这个简单的代码。但是我得到了一个我无法修复的错误。有人能帮帮我吗?
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 。我也尝试重新安装webdriver。已尝试定位路径。但我读到它已经没有必要了。我把Chromedriver放在Python项目的同一个目录中
项目+ Chromedriver的快照:

8mmmxcuj

8mmmxcuj1#

我把Chromedriver放在Python项目的同一个目录中
由于您使用的是Seleniumv4.11.2,您不需要显式下载ChromeDriverGeckoDriver或任何浏览器驱动程序,甚至不需要再使用webdriver_manager。您只需要确保所需的浏览器客户端,即。安装了google-chromefirefoxmicrosoft-edge

Selenium Manager

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

解决方案

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

from selenium import webdriver

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

tl; dr

Chrome for Testing: reliable downloads for browser automation

slsn1g29

slsn1g292#

你的电脑上安装了Chrome浏览器吗?chromedriver只是一个驱动程序。请安装Chrome浏览器和相应版本的chormedriver。

qfe3c7zg

qfe3c7zg3#

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

相关问题