Browser Automation with Python and Selenium — 1: Installation

In the previous post, I made a quick introduction to the Selenium project. We will look at the installation steps to get started in this post.

We need to install the Python language binding to use Selenium with Python in our automation project. Currently supported Python versions are 3.5 and above. In addition to that, we will need WebDriver binaries for the browsers we want to automate and run the commands on.

Installing the Selenium library

  • First things first, always use a virtual environment for your projects to isolate dependencies from the rest of the system.
python -m venv env
source env/bin/activate
  • Install the selenium library using pip.
pip install selenium
python install

Installing WebDriver binaries

Drivers control the actual browsers. They are mostly created by browser vendors. As stated in the official documentation

Drivers are generally executable modules that run on the system with the browser itself, not on the system executing the test suite. (Although those may be the same system.)

You need to have browser-specific WebDriver binaries installed to be able to control the browser.

There are 2 options to manage drivers: either manually or by using a package for this purpose.

1. Managing driver binaries with a python package

Through these series of posts, I will mostly use the webdriver-setup package written by me on top of the webdriver-manager to provide easy to use interface for webdriver binary download and instance creation.

  • Install the package with pip.
pip install webdriver-setup
  • Use it in your script as follow to download the binary and create the webdriver instance.
from webdriver_setup import get_webdriver_fordriver = get_webdriver_for("firefox")

2. Managing driver binaries manually

There are 2 steps for manual management:

  1. Download the WebDriver binary supported by your preferred browser from the links below.

Note that the Safari webdriver is built-in for El Capitan and newer versions of macOS.

2. Place the binary in a directory that is already in your system path or manually pass the directory path where you put your binary with executable_path argument.

For example

  • Create a directory to place the executables.
Linux/Mac: /opt/WebDriver/bin
Windows: C:\WebDriver\bin
  • Add the directory to your PATH.

For Linux/Mac, run the following from a terminal, and source the .profile or reopen the terminal to activate.

export PATH=$PATH:/opt/WebDriver/bin >> ~/.profile

For Windows, open a command prompt as administrator and run the following command to permanently add the directory to your path for all users on your machine.

setx /m path "%path%;C:\WebDriver\bin\"

You can put the binary under one of the paths currently in the system path like /usr/bin, /usr/local/bin for Linux/Mac.

As an example, if you run chromedriver from your terminal, you will get an output like below.

Starting ChromeDriver 86.0.4240.198 (d8a506935fc2273cfbac5e5b629d74917d9119c7-refs/branch-heads/4240@{#1431}) on port 9515
Only local connections are allowed.
Please see for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

But we won’t run it manually.

Creating driver instances

Creating an instance is common for all browsers. Replace <browser> in the following template with the browser interface you want to use like Firefox, Chrome, Opera, Safari, Edge, or Ie.

If you are using the webdriver-setup package, you can give the browser name as an argument to download its webdriver binary and create the instance.

from webdriver_setup import get_webdriver_fordriver = get_webdriver_for("chrome")

For Safari on macOS High Sierra and later, run the following command from the terminal for the first time to authorize WebDriver.

safaridriver --enable

Things to Remember

  • You need to download the specific webdriver binary for your browser of choice and update the system path if you put your binary in a directory that is not in your path by default. Alternatively, you can use a package to manage the download and creation of the webdriver instance in your code.
  • You need to install the selenium package.

In the next post, I will show and explain a simple example of webdriver usage.

Software Engineer, Python Enthusiast, Darbuka Player,

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store