4

I'm going to do Crawling through selenium on Linux.

However, an error message occurred.

error message

  File "craw_after1day.py", line 202, in <module>
    driver = webdriver.Chrome('/home/ec2-user/linux_chromedirver/chromedriver',options=options)
  File "/home/ec2-user/.local/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 98, in start
    self.assert_process_still_running()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
    % (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service /home/ec2-user/linux_chromedirver/chromedriver unexpectedly exited. Status code was: 127

Method attempted to resolve this error

  1. execute_path setting in chromedriver
  2. refer WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127

selenium sample code for use with linux

import json
import time
import requests
import pymysql

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


options = Options()

options.add_argument('lang=en') 
options.add_argument('--headless') 
options.add_argument('--no-sandbox')
options.add_argument('--single-process')
options.add_argument('--disable-dev-shm-usage')


# fake-user-agent를 추가
options.add_argument('user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/5$

# options.add_argument('lang=en') 와 같이 써줘야함. (바인딩 언어 옵션)
options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})


driver = webdriver.Chrome('/home/ec2-user/linux_chromedirver/chromedriver',options=options)

How can I run selenium on Linux?

Please help me.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
elk_basic
  • 153
  • 2
  • 2
  • 9

3 Answers3

6

How to install a chrome driver in Linux

For doing so, you need a headless browser.

1) You need to install Chrome Binary

# Install Chrome.
sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
sudo echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
# Update our system
sudo apt-get -y update
# Install Chrome
sudo apt-get -y install google-chrome-stable

2) Afterwards, you need to install Chrome Driver.

# Install Chromedriver
wget -N https://chromedriver.storage.googleapis.com/95.0.4638.54/chromedriver_linux64.zip -P ~/
unzip ~/chromedriver_linux64.zip -d ~/
# Remove zip file
rm ~/chromedriver_linux64.zip
# Move driver to bin location
sudo mv -f ~/chromedriver /usr/local/bin/chromedriver
# Give it rights
sudo chown root:root /usr/local/bin/chromedriver
sudo chmod 0755 /usr/local/bin/chromedriver

3) Install Selenium

# Install Selenium

pip install selenium

4) You are ready, just test a script.

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

# Set path Selenium
CHROMEDRIVER_PATH = '/usr/local/bin/chromedriver'
s = Service(CHROMEDRIVER_PATH)
WINDOW_SIZE = "1920,1080"

# Options
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(service=s, options=chrome_options)

# Get the response and print title
driver.get("https://www.python.org")
print(driver.title)
driver.close()

You have now selenium running in Linux. The script should give you:

>>> Welcome to Python.org

Keep an eye on the chrome driver version which should match the version of the chrome web browser. Give it a read here

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Geomario
  • 152
  • 1
  • 12
  • Here is also another source; https://linuxize.com/post/how-to-install-google-chrome-web-browser-on-ubuntu-18-04/ – msklc May 10 '22 at 08:49
2

Looks like a chrome driver mismatch issue.

Get yourself a latest version from here

and use it like below:

driver = webdriver.Chrome(executable_path='/path/to/chromedriver', options=options)
driver.get("http://www.python.org")
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
2

you can use

from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())

it will always run your code on the latest chromedriver.

theNishant
  • 663
  • 5
  • 15