Learn how to install Selenium for Python to automate website testing. Set up Selenium using our comprehensive guide to gain an advantage in automating web tasks.
OVERVIEW
Nowadays, automation has entered every industry, from software development to performing daily duties. So why not integrate automation into website testing? One such framework that offers an array of tools for automating web browsers is Selenium. This enables developers to mimic actions taken by users, interact with the web elements, and automate many different tasks.
Integrating Selenium in testing requires a programming language such as Python to write maintainable and scalable scripts. But why choose Python in the first place? Because Python has a solid ecosystem of modules and frameworks and is well renowned for being straightforward and readable. It becomes a powerful web automation tool when combined with Selenium, making it simple for developers to automate time-taking manual testing operations.
In this article, we are going to cover the prerequisites for installation. Then we will see how to install Selenium Python using different package managers like pip, Pipenv, Poetry, and Conda. Next, setting up the process and verification of Selenium will be covered.
Selenium is a popular open-source framework that facilitates automated web testing and scraping. It can control web browsers such as Chrome, Firefox, Safari, and Edge, enabling automated tasks like filling out forms, clicking buttons, navigating through pages, extracting data, and also facilitating tweaking browser preferences.
Let’s explore some key features of the Selenium framework:
Let's now discuss the benefits of utilizing Python with Selenium:
Now that we have seen the advantages of Selenium and Python, let’s go on a roller coaster ride to the installation process for Selenium Python.
To install Selenium in Python, you need to have Python installed on your system, along with pip (Python package manager). Here are the prerequisites and instructions for installing Python and pip on different operating systems:
Python comes pre-installed on macOS. However, it's recommended to install the latest version
sudo apt-get update
sudo apt-get install python3
After installing Python, you can proceed with installing Selenium by using different package managers, further discussed in the article.
Now that the latest version of Python is installed on your system. Let’s start with the installation process of the managers, and then we will see how to install Selenium Python in this section.
To install Selenium via pip, you first need to ensure that you have pip installed. If you haven’t installed pip yet, here's the process to install pip
To check if pip is already installed, open the command prompt or terminal and run the following command:
pip --version
If the pip is installed, the current version of the pip will be shown. If not, you will get an error message.
You can install it by following these steps:
Run the following command to install pip:
python get-pip.py
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
sudo python get-pip.py
Once pip is installed, you can proceed to install Selenium Python:
Run the following command to install Selenium:
pip install selenium
The above command will download and install the latest version of Selenium and its dependencies.
Note: The process to install Selenium is similar for all platforms, whether it is Windows, macOS, or Linux.
After the installation is complete, you can verify by running the following command:
pip show selenium
This command will display information about the installed Selenium package with its version like 4.x.x.
That's it! Selenium has now been set up using pip. You may begin automating and testing the web with Selenium in your Python projects.
Conada is the second most used package manager after pip. To install Selenium via Conda, you first need to ensure that you have Conda installed.
Here's the process for installing Conda:
To verify that Conda is installed correctly, run the following command:
conda --version
If Conda is installed, you will see the version. If not, ensure the installation directory is added to your system's PATH environment variable.
Once Conda is installed, you can proceed to install Selenium:
Open a command prompt or terminal.
Run the following command to create a new Conda environment and install Selenium Python:
conda create --name venv selenium
Replace venv with the desired name for your environment.
When prompted to proceed, type y and press Enter to confirm the installation.
Activate the Conda environment by running the following command:
conda activate venv
source activate venv
Replace venv with the name of your Conda environment.
You can now start using Selenium within the activated Conda environment for your Python projects.
That's it! Selenium has now been set up using Conda. Conda offers a tool for managing packages and environments that makes it simple to install and maintain dependencies like Selenium.
To install Selenium via Pipenv, you first need to ensure that you have Pipenv installed.
Here's the process to install Pipenv:
By opening a command prompt or terminal and entering the following command, you may determine if Pipenv is already installed:
pipenv --version
Pipenv's current version will be displayed if it has been set up. You will see an error message if it is not installed.
You can install it by following these steps:
Open a command prompt.
Run the following command to install Pipenv using pip:
pip install pipenv
Open a terminal.
Run the following command to install Pipenv using pip:
pip3 install pipenv
Once Pipenv is installed, you can proceed to install Selenium Python:
Run the following command to create a new Pipenv environment and install Selenium:
pipenv install selenium
This command will create a new virtual environment for your project and install Selenium and its dependencies inside that environment.
Note: The process to install Selenium is similar for all platforms, whether it is Windows, macOS, or Linux.
After the installation is complete, you can activate the Pipenv environment by running the following command:
pipenv shell
This command will activate the virtual environment and allow you to work within it.
Now you can start using Selenium in your Python projects with the activated Pipenv environment.
That's it! You have now installed Selenium via Pipenv. Pipenv provides an isolated and reproducible environment for your Python projects, making it easier to manage dependencies and project-specific configurations.
To install Selenium via Poetry, you first need to ensure that you have Poetry installed. Here's the process for installing Poetry:
Run the following command on a command prompt or terminal to see if Poetry is already installed:
poetry --version
If Poetry is installed, you will see the version. If not, you will get an error message.
If Poetry is not installed, you can install it by following these steps:
Open a command prompt.
Run the following command to install Poetry using pip:
pip install poetry
Open a terminal.
Run the following command to install Poetry using pip:
pip3 install poetry
Once Poetry is installed, you can proceed to install Selenium Python:
Run the following command to create a new Poetry project:
poetry new project_name
Change into the project directory by running the following command:
cd project_name
Add the following lines under the [tool.poetry.dependencies] section:
selenium = "^4.9.0"
This specifies that you want to install Selenium version 4.9.0 or higher. You can adjust the version constraint as needed.
Run the following command to install the project dependencies, including Selenium:
poetry install
This command will resolve and install the dependencies specified in the pyproject.toml file, including Selenium and its dependencies.
You can start using Selenium in your Python programs within the Poetry environment after the installation is finished.
Now that Selenium is installed and ready to use, let’s install WebDriver, which helps a user to control the web browser’s actions. We will learn more about it in the next section.
Selenium WebDriver serves as a bridge between the web browser and your automation script, which is typically written in a programming language like Python, Java, or any other. It supports various web browsers, including Chrome, Firefox, Safari, Edge, and others. Each browser requires a specific WebDriver implementation, known as a "driver." For instance, the ChromeDriver is used to automate the Google Chrome browser, while the GeckoDriver is used for Firefox.
In this section, we will see how to install WebDriver for different browsers and set it up for various operating systems.
WebDriver facilitates the following tasks:
Installation and setup of WebDriver for various browsers:
SafariDriver comes pre-installed with Safari on macOS and iOS.
Enable the Safari WebDriver in Safari's Develop menu by following these steps:
In your Python script, use webdriver.Safari() to use Safari’s WebDriver.
Now that the desired WebDriver is installed, let's set its environment variable path. The advantage of setting up the PATH in the Environment Variable is that it minimizes the need of mentioning it explicitly each time one uses that browser's WebDriver.
Windows:
macOS or Linux:
Open the Terminal and run the following command to give execution permissions to the WebDriver executable:
chmod +x /path/to/web driver/executable
To add the WebDriver executable to the PATH environment variable temporarily for the current session, run the following command:
export PATH=$PATH:/path/to/webdriver
If you want to make the PATH change permanent, add the export command to your shell configuration file (e.g., ~/.bash_profile or ~/.zshrc).
Remember to replace “/path/to/webdriver/executable” with the actual path to the WebDriver executable file.
Once the setup is done, you can instantiate the WebDriver in your Python scripts and start automating browser interactions using Selenium.
To make sure that your browser's WebDriver is installed correctly, open the command prompt for Windows or the terminal for macOS/ Linux and write the following command:
For ChromeDriver:
chromedriver
For Firefox WebDriver (GeckoDriver):
geckodriver
For Microsoft Edge WebDriver (msedgedriver):
msedgedriver
If the driver is correctly installed, it outputs the version of the WebDriver and executes that driver. For example:
Now that our drivers are successfully installed, let’s try to execute a script to automate a task which is to checkmark all the tasks and add some new tasks to a to-do website. The following script will do the task we aim for.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://lambdatest.github.io/sample-todo-app/")
driver.set_window_size(1400, 771)
driver.find_element(By.NAME, "li1").click()
driver.find_element(By.NAME, "li4").click()
driver.find_element(By.ID, "sampletodotext").click()
driver.find_element(By.ID, "sampletodotext").send_keys("Drink water")
driver.find_element(By.ID, "addbutton").click()
driver.find_element(By.ID, "sampletodotext").click()
driver.find_element(By.ID, "sampletodotext").send_keys("meeting with robert")
driver.find_element(By.ID, "addbutton").click()
element = driver.find_element(By.ID, "addbutton")
driver.find_element(By.NAME, "li6").click()
driver.find_element(By.NAME, "li7").click()
driver.find_element(By.NAME, "li2").click()
driver.find_element(By.NAME, "li3").click()
driver.find_element(By.NAME, "li5").click()
driver.quit()
Let’s understand the code step-by-step:
Till this point, a question might come to your mind, how is the website testing done on different platforms? Do organizations set up and maintain big infrastructure to test on various operating systems and browser versions?
The answer to both questions is a Big NO. It is not a feasible idea to maintain infrastructure to test applications. To leverage this, Cloud Grid is used.
A distributed computing system known as a "cloud grid" combines the strength of cloud resources with grid computing concepts to effectively handle difficult tasks. Cloud Grid offers parallel processing, scalability, and efficient resource usage by utilizing virtual machines or containers on cloud servers. LambdaTest is one of the Cloud Grid and cloud testing platforms that enables software testers and developers to do automated and manual testing across a wide range of browsers, operating systems, and mobile devices.
You could run into several frequent problems when installing Selenium, like version conflicts or WebDriver configuration issues. Here are some suggestions to diagnose and fix certain problems:
Version conflicts:
WebDriver configuration problems:
Firewall or proxy issues:
Dependencies and environment setup:
Logging and error messages:
This article provides an overview of installing Selenium with Python for web automation and testing purposes. It covers the prerequisites and steps for installing Selenium in Python on different operating systems, including Windows, macOS, and Linux.
Depending on the operating system, different package management systems, like pip, Pipenv, Poetry, and Conda, are used to describe the installation process. These package managers aim for seamless installation and efficient dependency management.
We looked at configuring WebDriver for several web browsers, including Chrome, Firefox, Microsoft Edge, and Safari. To guarantee a smooth installation, we also discussed how to verify the Selenium WebDriver installation and resolve typical problems that can come up while using or installing WebDriver.
To sum up, the article covers detailed installation processes needed to combine Selenium with Python and establish the groundwork for creating effective web automation and testing scripts. Users may automate browser interactions and carry out thorough web testing by utilizing Selenium and Python's capabilities.
On this page
Author's Profile
Jainish Patel
Jainish Patel is a tech enthusiast specializing in web development. With a primary focus on React.js, React.native, and core front-end languages, He is well-equipped to tackle the ever-evolving world of web technologies. Building sleek and interactive Mobile and web applications is one of his strengths. Moreover, he is passionate about crafting seamless user experiences and transforming ideas into reality through programming. You can also follow him on Twitter.
Reviewer's Profile
Shahzeb Hoda
Shahzeb currently holds the position of Senior Product Marketing Manager at LambdaTest and brings a wealth of experience spanning over a decade in Quality Engineering, Security, and E-Learning domains. Over the course of his 3-year tenure at LambdaTest, he actively contributes to the review process of blogs, learning hubs, and product updates. With a Master's degree (M.Tech) in Computer Science and a seasoned expert in the technology domain, he possesses extensive knowledge spanning diverse areas of web development and software testing, including automation testing, DevOps, continuous testing, and beyond.
Get 100 minutes of automation test minutes FREE!!