How to setup Selenium with Python ?

Selenium is a powerful browser automation toolset which facilitates interacting programatically with browsers.

What do you need to setup Selenium ?

Here is what they say in the documentation about setting up Selenium.

Selenium setup is quite different from the setup of other commercial tools. To use Selenium in your automation project you need to install the language bindings libraries for your language of choice. In addition you will need WebDriver binaries for the browsers you want to automate and run test on.

Selenium Website

I struggled to go over the installation stuff. Indeed, in most cases you just need a simple pip install some-package for most software to get them running. But Selenium deals with the browser, you will then need those « WebDriver binaries » to manipulate the browser you want.

In this article I will show how to setup Selenium with ChromeDriver in Python. (The process is similar for most Drivers).

Here are the steps to take:

  1. Install the Selenium library
  2. Download browser-specific WebDriver binaries
  3. Place the downloaded WebDriver binary in the System PATH

This step is optional but is recommended by Selenium and we will see why.

System PATH: What is it?

Things got a little tricky for me at the step 3 where you have to place files in System Path. Indeed I had no clue of what System PATH was.

Some(Some 😅) googling made me find out that the system PATH is a list of folders, separated by a semicolon on Windows or by a colon on Unix, that identifies the folders that the system should search when looking for files that are called from the command line, or other processes.

When a command name is specified by the user for example, the system searches through $PATH, examining each directory from left to right in the list, looking for a filename that matches the command name. Once found, the program is executed as a child process of the command shell or program that issued the command. An example is when you type python in the command line (assuming you have python installed) the system will search the $PATH variable and open the python executable.

On unix you can enter echo $PATH to see the list and echo %PATH% on Windows.
Here is what it might look like.

$ echo $PATH 
/home/users/etudiant/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/users/etudiant/WebDriver

Whenever you install a new program designed to be executed from a command window(Python, Node..) the system path is changed to include the program's installation path.

On the other side programs that are primarily designed to be used from Graphical User Interface(Browser, Video Player..) do not appear in the Path. That’s why some programs may add their directory to the front of the PATH variable's content during or after installation, to speed up the search process and/or override OS commands.

It turns out that you can use Selenium without adding the Webdriver binary in the system path. You can manually specify where the executable lives in your script before starting WebDriver. (See how it’s done further in the article)

However this can make your tests less portable as the executables will need to be in the same place on every machine(since you specified an unique path to the executable) or include the executable within your test code repository.

This is all the point of adding the folder containing the desired WebDriver’s binaries to our system’s path, Selenium will then be able to locate the additional binaries without requiring that you specify the exact location of the driver in your code.

That said, let's now install all we need to run Selenium.

Installation

Step 1: Install the Selenium package

On a terminal command run the following:

pip install selenium

Step 2: Download ChromeDriver Binary

Head to ChromeDriver site and get the ChromeDriver compatible with your Chrome version. For other browsers, you can find here links to download the corresponding drivers.

Once the driver binary downloaded, we create a new directory to place the executables in, like C:\Users\WebDriver or /users/WebDriver.

Step 3: Add the directory to the System PATH

  • On 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:\Users\WebDriver\"
  • On Unix systems

Open a command prompt as administrator and run the following command:

export PATH=$PATH:/users/WebDriver

Note however that this will add the directory to System PATH for only the current session. See below how to add the directory permanently in your path.

It is a hidden file and simple ls command won’t show the file.

Unix Systems Only

If you want add a directory permanently to your path add this line "export PATH=$PATH:/"path/to/folder"/ to your ~/.bashrc file.

The .bashrc file is a script file that’s executed when a user logs in. The file itself contains a series of configurations for the terminal session. This includes settings you want to use whenever you open a terminal.

So the goal is to add our export line to this file, so that whenever we open a terminal command or want to execute the Driver binary, the path to our Driver binary is already set. To do so:

  1. Type nano ~/.bashrc in the terminal
    This will open the .bashrc file.
    nano is an easy to use command line text editor for Unix and Linux operating systems. It includes all the basic functionality you'd expect from a regular text editor.
  2. Once the file opened:
  • Press Ctrl + w + v to go to the end of the file and add the line export PATH=$PATH:/"path/to/folder"/
  • Press Ctrl + O to save changes
  • And last press Ctrl + X to exit the file

You will need to log out and log back on, or run source ~/.profile to make your changes available to your bash environment.

Running Selenium

Type chromedriver in a terminal command.

(UNIX users) Make sure to log out and log back on or to run source ~/ .profile before typing this command.

If your PATH is configured correctly, you will see some output relating to the startup of the driver like this:

:~$ chromedriver
Starting ChromeDriver 87.0.4280.88 (89e2380a3e36c3464b5dd1302349b1382549290d-refs/branch-heads/4280@{#1761}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

If the output is somethink like command unfound, it means that the driver isn't added to System PATH. In that case check if you completed the above steps correctly.

Now that we have the ChromeDriver executable placed on sytem path, we should be able to execute the chromedriver executable from any directory.

Let’s see if everything worked.

On a new python file add the following:

from selenium.webdriver
import time
driver = Chrome()
driver.get("https://selenium.dev")
time.sleep(3)
driver.quit()

Save the file and in a terminal cd to the directory containing our selenium-script.py and run:

python selenium-script.py

If all went well the selenium website should appear for 3 seconds and quit.

warning

If the driver's executable is not on the sytem path this won’t work and you will see an error message saying it.

To run Selenium without adding the driver on path you will need to change the third line with this to specify where the driver binary leaves:

driver = Chrome(executable_path="C:\Users\ChromeDriver\chromedriver.exe")

That's It

You have Selenium set up. You can see Quick dive into Selenium to see more on what Selenium has to offer.

Have fun manipulating browsers 😉.