简体   繁体   English

OSError:[Errno 8] 执行格式错误:“chromedriver”在 Ubuntu 服务器上使用 Chromedriver

[英]OSError: [Errno 8] Exec format error: 'chromedriver' using Chromedriver on Ubuntu server

I'm trying to use Chromedriver with Ubuntu (AWS instance).我正在尝试将 Chromedriver 与 Ubuntu(AWS 实例)一起使用。 I've gotten Chromedriver to work no problem in a local instance, but having many, many issues doing so in a remote instance.我已经让 Chromedriver 在本地实例中正常工作,但在远程实例中却有很多很多问题。

I'm using the following code:我正在使用以下代码:

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)

However, I keep getting this error:但是,我不断收到此错误:

    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'

I believe I'm using the most updated version of Selenium, Chrome, and Chromedriver.我相信我正在使用 Selenium、Chrome 和 Chromedriver 的最新版本。

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

Selenium: Selenium:

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

And, finally, for Chromedriver, I'm almost certain I downloaded the most recent version here: https://chromedriver.storage.googleapis.com/index.html?path=78.0.3904.70/ .最后,对于 Chromedriver,我几乎可以肯定我在这里下载了最新版本: https://chromedriver.storage.googleapis.com/index.html?path=78.0.3904.70/ It's the mac_64 version (I'm using Ubuntu on a Mac).这是 mac_64 版本(我在 Mac 上使用 Ubuntu)。 I then placed chromedriver in /usr/bin , as I read that's common practice.然后我将chromedriver放在/usr/bin中,因为我读到这是常见的做法。

I have no idea why this isn't working.我不知道为什么这不起作用。 A few options I can think of:我能想到的几个选项:

  1. some sort of access issue?某种访问问题? I'm a beginner with command line and ubuntu - should I be running this as "root" user?我是命令行和 ubuntu 的初学者 - 我应该以“root”用户身份运行它吗?

  2. mis-match between Chromedriver and Chrome versions? Chromedriver 和 Chrome 版本不匹配? Is there a way to tell which chromedriver version I have for certain?有没有办法确定我有哪个chromedriver版本?

  3. I see that Chromedriver and Selenium are in different locations.我看到 Chromedriver 和 Selenium 位于不同的位置。 Selenium is in: Location: /home/ubuntu/.local/lib/python3.6/site-packages and I've moved chromedriver to: /usr/bin . Selenium 位于: Location: /home/ubuntu/.local/lib/python3.6/site-packages ,我已将chromedriver移至: /usr/bin Could this be causing problems?这会引起问题吗?

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

  1. Download Chrome: wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb下载 Chrome: wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
  2. Install Chrome: sudo dpkg -i google-chrome-stable_current_amd64.deb安装 Chrome: sudo dpkg -i google-chrome-stable_current_amd64.deb
  3. If you'll get error run: sudo apt-get -f install如果你会得到错误运行: sudo apt-get -f install
  4. Check Chrome: google-chrome --version检查 Chrome: google-chrome --version
  5. Download chromedriver for Linux : wget https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_linux64.zip下载 Linux 的chromedriverwget https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_linux64.zip
  6. Unzip chromedriver, install unzip sudo apt install unzip if required: unzip chromedriver_linux64.zip解压 chromedriver,安装 unzip sudo apt install unzip如果需要: unzip chromedriver_linux64.zip
  7. Move chromedriver to /usr/bin: sudo mv chromedriver /usr/bin/chromedriver将 chromedriver 移动到 /usr/bin: sudo mv chromedriver /usr/bin/chromedriver
  8. Check chromedriver, run command: chromedriver检查chromedriver,运行命令: chromedriver
  9. Install Java: sudo apt install default-jre安装 Java: sudo apt install default-jre
  10. Install Selenium: sudo pip3 install selenium安装 Selenium: sudo pip3 install selenium

Create test file, nano test.py with content below.创建测试文件, nano test.py ,内容如下。 Press CTRL+X to exit and the Y to save.按 CTRL+X 退出,按 Y 保存。 Execute your script - python3 test.py执行你的脚本 - 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()

Example of using Docker and selenium/standalone-chrome-debug:使用 Docker 和 selenium/standalone-chrome-debug 的示例:

  1. Install docker, installation steps are here安装docker,安装步骤在这里
  2. Start container, using sudo docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:3.141.59-xenon command, different options are here启动容器,使用sudo docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:3.141.59-xenon命令,这里有不同的选项
  3. Open Security Group of your instance in AWS and add TCP rule to be able to connect.在 AWS 中打开您的实例的安全组并添加 TCP 规则以便能够连接。 You can add only your own IP and port 4444 for Selenium您只能为 Selenium 添加自己的 IP 和端口 4444
  4. Run test from local从本地运行测试
    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())

I am running the following on ec2-ubuntu :我在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

Try it.试试看。 Incase it doesn't work I will find more of the settings.万一它不起作用,我会找到更多设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OSError:[Errno 8]尝试运行chromedriver for selenium时出现exec格式错误 - OSError: [Errno 8] Exec format error when trying to run chromedriver for selenium Python Selenium chromedriver OSError:[Errno 8] Exec格式错误 - Python Selenium chromedriver OSError: [Errno 8] Exec format error OSError: [Errno 8] 执行格式错误 - OSError: [Errno 8] Exec format error OSError:[Errno 8]尝试使用浏览器启动服务器时出现格式错误 - OSError: [Errno 8] Exec format error when trying to start server using browsermobproxy python-OSError:[Errno 8]执行格式错误 - python - OSError: [Errno 8] Exec format error OSError:[Errno 8] Exec格式错误 - python - OSError: [Errno 8] Exec format error - python Pdfkit / wkhtmltopdf-OSError:[Errno 8] Exec格式错误 - Pdfkit/wkhtmltopdf - OSError: [Errno 8] Exec format error OSError:[Errno 8] Exec格式错误selenium - OSError: [Errno 8] Exec format error selenium FileNotFoundError:[Errno 2] 没有这样的文件或目录:&#39;/Users/fuadhafiz/Documents\\\\chromedriver.exec&#39; 在 MAC OSX 上使用 Selenium 时出错 - FileNotFoundError: [Errno 2] No such file or directory: '/Users/fuadhafiz/Documents\\chromedriver.exec' error using Selenium on MAC OSX Errno 8 EXEC 格式错误在 FF 81 Ubuntu 上使用 geckodriver 28 - Errno 8 EXEC Format Error using geckodriver 28 on FF 81 Ubuntu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM