Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to install Chrome, ChromeDriver and Selenium on CentOS. Plus a sample scraping script.
#!/usr/bin/env bash
# https://developers.supportbee.com/blog/setting-up-cucumber-to-run-with-Chrome-on-Linux/
# https://gist.github.com/curtismcmullan/7be1a8c1c841a9d8db2c
# http://stackoverflow.com/questions/10792403/how-do-i-get-chrome-working-with-selenium-using-php-webdriver
# http://stackoverflow.com/questions/26133486/how-to-specify-binary-path-for-remote-chromedriver-in-codeception
# http://stackoverflow.com/questions/40262682/how-to-run-selenium-3-x-with-chrome-driver-through-terminal
# http://askubuntu.com/questions/760085/how-do-you-install-google-chrome-on-ubuntu-16-04
# Versions
CHROME_DRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`
SELENIUM_STANDALONE_VERSION=3.8.1
SELENIUM_SUBDIR=$(echo "$SELENIUM_STANDALONE_VERSION" | cut -d"." -f-2)
# Remove existing downloads and binaries so we can start from scratch.
sudo yum remove google-chrome-stable
rm ~/selenium-server-standalone-*.jar
rm ~/chromedriver_linux64.zip
sudo rm /usr/local/bin/chromedriver
sudo rm /usr/local/bin/selenium-server-standalone.jar
# Install dependencies.
sudo yum update
sudo yum install -y unzip openjdk-8-jre-headless xvfb libxi6 libgconf-2-4
# Install Chrome.
vi /etc/yum.repos.d/google-chrome.repo
# Make sure you have below info in the file(remove hash).
#[google-chrome]
#name=google-chrome
#baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64
#enabled=1
#gpgcheck=1
#gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
yum install -y google-chrome-stable
# Install ChromeDriver.
wget -N https://chromedriver.storage.googleapis.com/2.35/chromedriver_linux64.zip -P ~/
unzip ~/chromedriver_linux64.zip -d ~/
rm ~/chromedriver_linux64.zip
sudo mv -f ~/chromedriver /usr/local/bin/chromedriver
sudo chown root:root /usr/local/bin/chromedriver
sudo chmod 0755 /usr/local/bin/chromedriver
# Install Selenium.
wget -N http://selenium-release.storage.googleapis.com/$SELENIUM_SUBDIR/selenium-server-standalone-$SELENIUM_STANDALONE_VERSION.jar -P ~/
sudo mv -f ~/selenium-server-standalone-$SELENIUM_STANDALONE_VERSION.jar /usr/local/bin/selenium-server-standalone.jar
sudo chown root:root /usr/local/bin/selenium-server-standalone.jar
sudo chmod 0755 /usr/local/bin/selenium-server-standalone.jar
#!/usr/bin/env bash
import time
from selenium import webdriver
import os
from selenium.webdriver.common.keys import Keys
# "$ xvfb-run python test.py", this is how you run this script
chrome_options = webdriver.ChromeOptions()
# below trick saved my life
chrome_options.add_argument('--no-sandbox')
# set the folder where you want to save your file
prefs = {'download.default_directory' : os.getcwd()}
chrome_options.add_experimental_option('prefs', prefs)
# Optional argument, if not specified will search path.
driver = webdriver.Chrome('/usr/local/bin/chromedriver',chrome_options=chrome_options)
chrome_options=chrome_options
# Scraping steps
driver.get("http://pypi.python.org/pypi/selenium")
time.sleep(3)
driver.find_element_by_css_selector("#content > div.section > table > tbody > tr.odd > td:nth-child(1) > span > a:nth-child(1)").click()
time.sleep(3)
print('Finished!')
driver.quit()
@zakabluk

This comment has been minimized.

Copy link

@zakabluk zakabluk commented Oct 5, 2018

This doesn't work for CentOS 7.5.1804. These packages xvfb libxi6 libgconf-2-4 not exist in standard repository. Need add changes.

@paulalex

This comment has been minimized.

Copy link

@paulalex paulalex commented Oct 10, 2018

This worked nicely for me ran as a shell provisioner for packer, I am bootstrapping the centos installation with a kickstart file. Happy to give you the scripts if you are interested.

@shtiyu

This comment has been minimized.

Copy link

@shtiyu shtiyu commented Dec 5, 2018

worked fine for me, thx!!

@Coldsp33d

This comment has been minimized.

Copy link

@Coldsp33d Coldsp33d commented Apr 12, 2019

@paulalex yes, please!

@briansanchez

This comment has been minimized.

Copy link

@briansanchez briansanchez commented Aug 10, 2019

thank you!.. for sharing.

@evandonovan

This comment has been minimized.

Copy link

@evandonovan evandonovan commented Dec 4, 2019

On CentOS 7.7.1908, I didn't need the following packages: libxi6 libgconf-2-4.

The X11 packages I needed were:

yum install xorg-x11-server-Xvfb dbus-x11

The former of those installed various X11 dependencies. I'm not 100% sure that the dbus one was needed.

I had already installed Selenium for Python via Pip using:

pip install selenium

Therefore, I didn't need all the Selenium standalone instructions, just the instructions on getting the Chromedriver itself.

Note: this issue explains the reason for the --no-sandbox parameter: theintern/intern#878

@8key

This comment has been minimized.

Copy link

@8key 8key commented Oct 15, 2020

It was useful, thank you!

@8key

This comment has been minimized.

Copy link

@8key 8key commented Oct 15, 2020

DO you know what --no-sandbox does?

@bhuang27

This comment has been minimized.

Copy link

@bhuang27 bhuang27 commented Mar 11, 2021

Hi, I get a 404 error when trying to access "baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64". Is the baseurl necessary to install Chrome?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment