Installing Selenium

Introduction

After installing Python and pip, the next step is to install the Selenium library. Selenium is distributed as a Python package and can be easily installed using pip, Python’s package manager.

Once Selenium is installed, you can write Python scripts to automate web browsers such as Google Chrome, Microsoft Edge, Mozilla Firefox, and Safari.

In this tutorial, you’ll learn how to install Selenium on Windows, macOS, and Linux, verify the installation, upgrade Selenium, and troubleshoot common installation issues.


What is the Selenium Package?

The Selenium package is a Python library that provides the APIs required to automate web browsers.

It enables you to:

  • Launch web browsers

  • Interact with web elements

  • Perform mouse and keyboard actions

  • Handle alerts and frames

  • Execute JavaScript

  • Capture screenshots

  • Automate complete web application workflows

The Selenium package works together with browser drivers (or Selenium Manager in Selenium 4.6+) to communicate with web browsers.


Prerequisites

Before installing Selenium, make sure you have:

  • Python installed

  • pip installed

  • Internet connection

  • A supported web browser (Chrome, Edge, Firefox, or Safari)


Installing Selenium on Windows

Open Command Prompt and run:

pip install selenium

pip will automatically download and install the latest stable version of Selenium.


Installing Selenium on macOS

Open Terminal and run:

pip3 install selenium

If your system maps Python 3 to the python command, you can also use:

pip install selenium

Installing Selenium on Linux

Open Terminal and run:

pip3 install selenium

On some Linux distributions, you may use:

pip install selenium

depending on your Python configuration.


Verify Selenium Installation

After installation, verify that Selenium has been installed successfully.

Run the following command:

Windows

pip show selenium

macOS / Linux

pip3 show selenium

Example Output

Name: selenium
Version: 4.x.x
Summary: Python bindings for Selenium

If package information is displayed, Selenium has been installed successfully.


Verify Using Python

Open the Python interpreter.

Windows

python

macOS / Linux

python3

Now import Selenium:

import selenium

print(selenium.__version__)

Example Output

4.x.x

If no error occurs and the version number is displayed, Selenium is ready to use.


Upgrading Selenium

It is recommended to use the latest stable version of Selenium.

Windows

pip install --upgrade selenium

macOS / Linux

pip3 install --upgrade selenium

Uninstalling Selenium

If you need to remove Selenium from your system:

Windows

pip uninstall selenium

macOS / Linux

pip3 uninstall selenium

Installing a Specific Version

Sometimes projects require a specific Selenium version.

Example:

pip install selenium==4.21.0

Replace the version number with the version required by your project.


Checking Installed Packages

To view all installed Python packages:

Windows

pip list

macOS / Linux

pip3 list

You should see Selenium listed among the installed packages.


Common Installation Issues

ModuleNotFoundError

Error

ModuleNotFoundError: No module named 'selenium'

Cause

Selenium is not installed in the active Python environment.

Solution

Install Selenium using:

pip install selenium

pip Command Not Found

Error

'pip' is not recognized as an internal or external command

Solution

Ensure pip is installed correctly and added to the system PATH before installing Selenium.


Multiple Python Versions

If multiple Python versions are installed, Selenium may be installed for a different version.

Use:

python -m pip install selenium

or

python3 -m pip install selenium

to install Selenium for the correct Python interpreter.


Best Practices

  • Always install the latest stable version of Selenium unless your project requires a specific version.

  • Verify the installation after installing the package.

  • Keep Selenium updated to access new features and bug fixes.

  • Use virtual environments for managing project dependencies.

  • Install Selenium using the same Python interpreter that will run your automation scripts.


Frequently Asked Questions (FAQs)

What is the Selenium package?

The Selenium package is a Python library that allows you to automate web browsers using Selenium WebDriver.


Do I need pip to install Selenium?

Yes. Selenium is installed using Python’s package manager, pip.


Is Selenium free?

Yes. Selenium is completely open-source and free to use.


How do I verify that Selenium is installed?

Run:

pip show selenium

or import Selenium in Python:

import selenium

print(selenium.__version__)

Can I install a specific Selenium version?

Yes.

Example:

pip install selenium==4.21.0

Key Takeaways

  • Selenium is installed as a Python package using pip.

  • The installation process is similar across Windows, macOS, and Linux.

  • Verify the installation using pip show selenium or by importing Selenium in Python.

  • Keep Selenium updated to use the latest features and improvements.

  • Use the same Python environment for installing and running Selenium.

  • Installing Selenium completes an important step in preparing your Selenium with Python automation environment.