为什么 Selenium 会抛出“Exec 格式错误”?

问题描述 投票:0回答:1

此代码抛出异常:

from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://www.google.com')
time.sleep(10)

这是回溯:

Traceback (most recent call last):
  File "/home/orangepi/.local/lib/python3.10/site-packages/selenium/webdriver/common/selenium_manager.py", line 134, in run
    completed_proc = subprocess.run(args, capture_output=True)
  File "/usr/lib/python3.10/subprocess.py", line 503, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib/python3.10/subprocess.py", line 971, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.10/subprocess.py", line 1863, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/home/orangepi/.local/lib/python3.10/site-packages/selenium/webdriver/common/linux/selenium-manager'

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

Traceback (most recent call last):
  File "/home/orangepi/.local/lib/python3.10/site-packages/selenium/webdriver/common/driver_finder.py", line 38, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
  File "/home/orangepi/.local/lib/python3.10/site-packages/selenium/webdriver/common/selenium_manager.py", line 103, in driver_location
    output = self.run(args)
  File "/home/orangepi/.local/lib/python3.10/site-packages/selenium/webdriver/common/selenium_manager.py", line 140, in run
    raise WebDriverException(f"Unsuccessful command executed: {command}") from err
selenium.common.exceptions.WebDriverException: Message: Unsuccessful command executed: /home/orangepi/.local/lib/python3.10/site-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/orangepi/Downloads/from selenium import webdriver.py", line 3, in <module>
    driver = webdriver.Chrome()
  File "/home/orangepi/.local/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(
  File "/home/orangepi/.local/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 49, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
  File "/home/orangepi/.local/lib/python3.10/site-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

我不知道我哪里出了问题:chrome已安装;我尝试了一些教程但没有成功。

python ubuntu selenium-webdriver web-scraping
1个回答
0
投票

Selenium 在路径变量中搜索 chrome 驱动程序,您可以手动添加它或在代码中设置驱动程序。

path var

这个问题通常可以通过下载 chromium 驱动程序轻松解决 https://developer.chrome.com/docs/chromedriver/downloads?hl=fr

然后您可以在代码或环境变量中指定该驱动程序的路径

driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe')

最终代码:

from selenium import webdriver

# Optional argument : if not specified WebDriver will search your system PATH environment variable for locating the chromedriver
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()
© www.soinside.com 2019 - 2024. All rights reserved.