3

I'm trying to use Chromedriver with Ubuntu (AWS instance). I've gotten Chromedriver to work no problem in a local instance, but having many, many issues doing so in a remote instance.

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.

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

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/. It's the mac_64 version (I'm using Ubuntu on a Mac). I then placed chromedriver in /usr/bin , as I read that's common practice.

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?

  2. mis-match between Chromedriver and Chrome versions? Is there a way to tell which chromedriver version I have for certain?

  3. I see that Chromedriver and Selenium are in different locations. Selenium is in: Location: /home/ubuntu/.local/lib/python3.6/site-packages and I've moved chromedriver to: /usr/bin . Could this be causing problems?

halfer
  • 19,824
  • 17
  • 99
  • 186
DiamondJoe12
  • 1,879
  • 7
  • 33
  • 81

2 Answers2

7

Ubuntu Server 18.04 LTS (64-bit Arm):

  1. Download 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
  3. If you'll get error run: sudo apt-get -f install
  4. Check Chrome: google-chrome --version
  5. Download chromedriver for Linux: wget 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
  7. Move chromedriver to /usr/bin: sudo mv chromedriver /usr/bin/chromedriver
  8. Check chromedriver, run command: chromedriver
  9. Install Java: sudo apt install default-jre
  10. Install Selenium: sudo pip3 install selenium

Create test file, nano test.py with content below. Press CTRL+X to exit and the Y to save. Execute your script - 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:

  1. Install docker, installation steps are here
  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
  3. Open Security Group of your instance in AWS and add TCP rule to be able to connect. You can add only your own IP and port 4444 for Selenium
  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())

Sers
  • 12,047
  • 2
  • 12
  • 31
  • Sers - thanks so much. You were the only one who was able to completely answer this question, and it's working now. My error was a combination of a few things: I had not installed chrome on ubuntu (most obvious error), plus I was not using the linux version of chromedriver. Thanks again! – DiamondJoe12 Nov 02 '19 at 16:27
  • Additionally, there was one line of additional code I had to add to get this working completely, and that was to add an extra chrome option: options.add_argument('--window-size=1420,1080') – DiamondJoe12 Nov 02 '19 at 16:28
  • While this code works (sometimes), I still frequently get the following error, which may merit a new question topic: selenium.common.exceptions.SessionNotCreatedException: Message: session not created from disconnected: unable to connect to renderer (Session info: headless chrome=78.0.3904.70) – DiamondJoe12 Nov 02 '19 at 16:33
  • Sers, the new question I have asked, regarding this issue, can be found here: https://stackoverflow.com/questions/58673231/chromedriver-on-ubuntu-selenium-common-exceptions-sessionnotcreatedexception-m – DiamondJoe12 Nov 02 '19 at 16:57
1

I am running the following on 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.

Sid
  • 3,749
  • 7
  • 29
  • 62