【问题标题】:OSError: [Errno 8] Exec format error: 'chromedriver' using Chromedriver on Ubuntu serverOSError: [Errno 8] Exec format error: 'chromedriver' using Chromedriver on Ubuntu server
【发布时间】:2026-01-19 06:20:14
【问题描述】:

我正在尝试将 Chromedriver 与 Ubuntu(AWS 实例)一起使用。我已经让 Chromedriver 在本地实例中正常工作,但在远程实例中却有很多很多问题。

我正在使用以下代码:

options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--remote-debugging-port=9222")

driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', chrome_options=options)

但是,我不断收到此错误:

    Traceback (most recent call last):
  File "test.py", line 39, in <module>
    driver = webdriver.Chrome()
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: 'chromedriver'

我相信我使用的是 Selenium、Chrome 和 Chromedriver 的最新版本。

Chrome版本为:Version 78.0.3904.70 (Official Build) (64-bit)

硒:

ubuntu@ip-172-31-31-200:/usr/bin$ pip3 show selenium
Name: selenium
Version: 3.141.0
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
License: Apache 2.0
Location: /home/ubuntu/.local/lib/python3.6/site-packages
Requires: urllib3

最后,对于 Chromedriver,我几乎可以肯定我在这里下载了最新版本:https://chromedriver.storage.googleapis.com/index.html?path=78.0.3904.70/。它是 mac_64 版本(我在 Mac 上使用 Ubuntu)。然后我将 chromedriver 放在 /usr/bin 中,因为我读到这是常见的做法。

我不知道为什么这不起作用。我能想到的几个选项:

  1. 某种访问问题?我是命令行和 ubuntu 的初学者 - 我应该以“root”用户身份运行它吗?

  2. Chromedriver 和 Chrome 版本不匹配?有没有办法确定我使用的是哪个chromedriver 版本?

  3. 我看到 Chromedriver 和 Selenium 位于不同的位置。 Selenium 位于:Location: /home/ubuntu/.local/lib/python3.6/site-packages,我已将 chromedriver 移至:/usr/bin。这会导致问题吗?

【问题讨论】:

  • 你在你的mac中使用虚拟机吗?
  • 我认为您在 32 位实例上使用 x64 chromedriver
  • 是的,我在 AWS 服务 (EC2) 上使用 Ubuntu。我应该使用哪个 chromedriver?选项在这里:chromedriver.storage.googleapis.com/…
  • 我不认为这是一个 32 位实例。这是我选择的版本:Ubuntu Server 18.04 LTS (HVM), SSD Volume Type - ami-0d5d9d301c853a04a (64-bit x86) / ami-0fb0129cd568fe35f (64-bit Arm)
  • 我是否正确,这是一个 64 位实例,如果我使用的是 Mac,我应该下载 Mac 64 位版本?

标签: python selenium ubuntu amazon-ec2 selenium-chromedriver


【解决方案1】:

Ubuntu 服务器 18.04 LTS(64 位 Arm):

  1. 下载Chrome:wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
  2. 安装 Chrome:sudo dpkg -i google-chrome-stable_current_amd64.deb
  3. 如果运行出错:sudo apt-get -f install
  4. 检查铬:google-chrome --version
  5. Linux 下载 chromedriver:wget https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_linux64.zip
  6. 解压chromedriver,安装解压sudo apt install unzip如果需要:unzip chromedriver_linux64.zip
  7. 将 chromedriver 移动到 /usr/bin:sudo mv chromedriver /usr/bin/chromedriver
  8. 检查chromedriver,运行命令:chromedriver
  9. 安装Java:sudo apt install default-jre
  10. 安装 Selenium:sudo pip3 install selenium

创建测试文件nano test.py,内容如下。按 CTRL+X 退出,按 Y 保存。执行你的脚本 - python3 test.py

#!/usr/bin/python3

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--remote-debugging-port=9222")

try:
  driver = webdriver.Chrome(chrome_options=options)
  driver.get("https://www.google.com")
  s = driver.find_element_by_name("q")
  assert s.is_displayed() is True
  print("ok")
except Exception as ex:
  print(ex)

driver.quit()

使用 Docker 和 selenium/standalone-chrome-debug 的示例:

  1. 安装docker,安装步骤为here
  2. 启动容器,使用sudo docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:3.141.59-xenon命令,不同选项为here
  3. 在 AWS 中打开您的实例的安全组并添加 TCP 规则以便能够连接。您只能为 Selenium 添加您自己的 IP 和端口 4444
  4. 从本地运行测试
    options = webdriver.ChromeOptions()
    options.add_argument('--no-sandbox')
    options.add_argument('--headless')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument("--remote-debugging-port=9222")
    driver = webdriver.Remote(command_executor="http://your_instance_ip:4444/wd/hub",
                              desired_capabilities=options.to_capabilities())

【讨论】:

  • Sers - 非常感谢。你是唯一能够完全回答这个问题的人,而且它现在正在工作。我的错误是几件事的结合:我没有在 ubuntu 上安装 chrome(最明显的错误),而且我没有使用 chromedriver 的 linux 版本。再次感谢!
  • 此外,我必须添加一行额外代码才能使其完全正常工作,那就是添加一个额外的 chrome 选项:options.add_argument('--window-size=1420, 1080')
  • 虽然此代码有效(有时),但我仍然经常收到以下错误,这可能值得一个新的问题主题:selenium.common.exceptions.SessionNotCreatedException:消息:会话未从断开连接创建:无法连接到渲染器(会话信息:headless chrome=78.0.3904.70)
  • Sers,我提出的关于这个问题的新问题,可以在这里找到:*.com/questions/58673231/…
【解决方案2】:

我在ec2-ubuntu 上运行以下命令:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.headless = True

driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", chrome_options=options) #Give the full path to chromedriver

试试看。万一它不起作用,我会找到更多设置。

【讨论】: