Table of Contents

How To Install Selenium Chrome On Linux

Last Updated: 2023-09-28

Installing Chrome and Selenium can be quite challenging. This post goes through step by step tutorial to install Selenium and Chrome. I have also put together a small section at the end to cover "most commonly errors" during installation. Lets get started now.

Lets install first Chrome.

How to install Chrome On Centos 7

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

Lets install chrome now.

yum localinstall google-chrome-stable_current_x86_64.rpm

Lets check if chrome is installed.

google-chrome --version
Google Chrome 79.0.3945.130

I will be using Python Selenium for this post.

How to install Python Selenium

Lets install Selenium first using pip.

pip install selenium
Collecting selenium
  Using cached https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl
Requirement already satisfied: urllib3 in /home/anaconda3/lib/python3.7/site-packages (from selenium) (1.24.2)
Installing collected packages: selenium
Successfully installed selenium-3.141.0

We also need to install package chromedriver. Install it using yum.

yum install chromedriver

Lets try using the Selenium now in Ipython. Import following packages in ipython.

from selenium import webdriver

I am running Chrome as headless on Centos. So we need to add following options before initializing the webdriver.

chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("--headless")
chromeOptions.add_argument("--remote-debugging-port=9222")
chromeOptions.add_argument('--no-sandbox')

Lets initialize Chrome webdriver now.

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

Commonly encountered errors while intializing Chrome webdriver

Please do following if you encounter any one of the following errors while initializing chrome webdriver.

If you got following error. It means either your chrome installation is not done or not correct. Check it and make sure it is installed correctly.

WebDriverException: Message: unknown error: cannot find Chrome binary

If you get following. Make sure --headless option is added.

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally

To fix following error. Make sure --remote-debugging-port option is added.

unknown error: DevToolsActivePort file doesn't exist

To fix following error, make sure --no-sandbox option is added as shown above.

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (chrome not reachable)

If you see following error about chrome version that means your Chromedriver and Chrome versions are out of sync.

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 79 

Go here and find the correct version of ChromeDriver,

https://chromedriver.storage.googleapis.com/index.html

unzip the file and move the file to /usr/bin directory...

sudo mv -f chromedriver /usr/bin/chromedriver

How To Install Selenium Chrome On Ubuntu

Install the latest Google Chrome stable version from https://www.ubuntuupdates.org/pm/google-chrome-stable, As of writing this article - Google Chrome stable version 117 is the latest one.

wget http://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_117.0.5938.132-1_amd64.deb
sudo dpkg -i google-chrome-stable_117.0.5938.132-1_amd64.deb

Install the latest chromedriver

wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/117.0.5938.92/linux64/chromedriver-linux64.zip
unzip chromedriver-linux64.zip
sudo mv chromedriver-linux64/chromedriver /usr/bin/

You can test in Selenium 4.10 and above ...

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

service = Service(executable_path=r'/usr/bin/chromedriver')
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=service, options=options)

Related Posts

1
2
3
4
5