How To Install And Use Python Selenium Chrome WebDriver

Python crawler can crawl not only static web pages but also dynamic web pages. However, the new version of selenium doesn’t support PhantomJS and can’t crawl dynamic web pages. Therefore, we should abandon PhantomJS and use headless ChromeDriver directly. This article will tell you how to install ChromeDriver.

1. Why Install ChromeDriver.

The python third-party selenium library needs ChromeDriver to run the Google Chrome web browser.

2. Where To Download ChromeDriver.

You can download the google chrome driver executable file from its official download website. It provides both Linux, Windows, macOS versions. You should select the ChromeDriver version which matches the installed google chrome version on your OS. It is a zip file, after download, you need to unzip it to get the google chrome driver executable file, please remember the ChromeDriver executable file path carefully, we will use it later. And then you can use it in your python script.

3. Install Google ChromeDriver On macOS.

If your os is macOS, you can install follow below commands also.

  1. Run command brew install chromedriver in a terminal.
  2. If the above command failed, you can run the command brew cask install chromedriver in a terminal to install again.
  3. Make sure the python selenium package has been installed on your os with the command pip show selenium, if the python selenium package is not installed then run the command pip install selenium to install it.
    $ pip show selenium
    WARNING: Package(s) not found: selenium
    (base) songs-MacBook-Pro:~ songzhao$ pip install selenium
    Collecting selenium
      Downloading selenium-3.141.0-py2.py3-none-any.whl (904 kB)
         |████████████████████████████████| 904 kB 11 kB/s 
    Requirement already satisfied: urllib3 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from selenium) (1.25.3)
    Installing collected packages: selenium
    Successfully installed selenium-3.141.0
    (base) songs-MacBook-Pro:~ songzhao$ pip 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: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
    Requires: urllib3
    Required-by: 
    
  4. Verify the google ChromeDriver installation by executing the below python script in the python interaction console. If you see a blank google chrome web browser, that means the google ChromeDriver has been installed successfully, you can use it in your python selenium script.
    >>> from selenium import webdriver
    
    >>> browser = webdriver.Chrome(executable_path = '/Users/songzhao/Downloads/chromedriver') # The executable_path is the google chromedriver executable file saved path.
  5. You can also add the google ChromeDriver executable file path in the OS PATH environment variable value, then you can run google chrome web browser like below.
    >>> from selenium import webdriver 
    
    >>> browser = webdriver.Chrome()
  6. If you meet an error message like below, it means the google chrome driver version does not match the google chrome web browser version, you should download the google chrome driver which version matches your installed google chrome web browser.
    selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 89
    Current browser version is 88.0.4324.150 with binary path /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.