How to Install Selenium in Python?
Last Updated :
20 Sep, 2024
Selenium Scripts are built to do some tedious tasks that can be automated using headless web browsers. For example, Searching for some Questions on Different Search engines and storing results in a file by visiting each link. This task can take a long for a normal human being but with the help of selenium scripts, one can easily do it.
Now, some of you may be wondering what is headless web browsers. It's nothing but a browser that can be controlled using these selenium scripts for automation(web tasks). Selenium Scripts can be programmed using various languages such as JavaScript, Java, Python, etc.
How to Use Selenium with Python and Linux Environment
Python should already be installed. It can be 2.* or 3.* version.
Steps:
Prerequisites: Python Installation
Before diving into Selenium, ensure that Python is installed on your system. Python 2.x or 3.x versions are both supported, although Python 3.x is recommended for new projects.
To check if Python is installed on your Linux system, use the following command:
python --version
If Python is not installed, you can easily install it using APT:
sudo apt install python3
Step 1: Installing Selenium in Python
Whatever Operating System You are Using Python command is Same for Installing Selenium Library.
Method 1: Installing Selenium Using pip
The easiest way to install Selenium is through Python’s package manager pip. Open your terminal and run the following command:
python -m pip install selenium
This will install the latest version of Selenium and all its dependencies.
Method 2: Installing Selenium Manually
Alternatively, you can download the source distribution here, unarchive it, and run the command below:
python setup.py install
Step 2: Installing Web Drivers for Selenium
To use Selenium effectively, you need to install web drivers that allow your Python scripts to control browsers. Selenium supports several browsers such as Firefox, Chrome, and Edge. Here, we’ll focus on setting up GeckoDriver for Firefox, although the steps are similar for other browsers like Chromium.
- for using Firefox you may need to install GeckoDriver
- for using Chrome you may need to install Chromium
Installing GeckoDriver for Firefox in Linux
1. Download GeckoDriver: Go to the geckodriver releases page. Find the latest version of the driver for your platform and download it.
For example:
wget https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux64.tar.gz
2. Extract the file: Once downloaded, extract the tar file using the following command
tar -xvzf geckodriver*
3. Make it executable: Set the appropriate permissions to make it executable
chmod +x geckodriver
4. Move Files to usr/local/bin: Move the extracted geckodriver file to the '/usr/local/bin/' directory to make it globally accessible:
sudo mv geckodriver /usr/local/bin/
Installing GeckoDriver for Firefox in Windows
1. Same as Step 1 in Linux Download the GeckoDriver.
2. Extract it using WinRar or any application you may have.
3. Add it to Path using Command Prompt
setx path "%path%;GeckoDriver Path"
For Example:
setx path "%path%;c:/user/eliote/Desktop/geckodriver-v0.26.0-win64/geckodriver.exe"
Step 3: Creating a Simple Selenium Script in Python
Now that Selenium and the web drivers are installed, let’s create a simple Python script to automate a basic web task, such as opening a website, searching for a query, and fetching the page title.
Python
# Python program to demonstrate
# selenium
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://google.co.in")
Output:

where,
- We start by importing the required modules from Selenium.
- The webdriver.Firefox() initializes the Firefox browser.
- The script prints the title of the resulting page and then closes the browser.
Conclusion
Here, we’ve shown how to install Selenium, set up web drivers like GeckoDriver and ChromeDriver, and create a basic Selenium Python script on Linux. With Selenium’s powerful automation capabilities, you can streamline web-based tasks and testing processes, making your workflow faster and more efficient.
Similar Reads
Gmail Login using Python Selenium
Python scripting is one of the most intriguing and fascinating things to do meanwhile learning Python. Automation and controlling the browser is one of them. In this particular article, we will see how to log in to the Gmail account using Python and the power of selenium. Selenium automates and cont
4 min read
Install Coverage in Python
Understanding the execution of code, during testing is vital in software development. Code coverage plays a role in providing insights to developers regarding the portions of their code that are executed. By identifying areas of code that have not been tested developers can improve the quality and r
2 min read
Why do people prefer Selenium with Python?
Selenium is a strong set of tools that firmly supports the quick development of test automation of web applications. It offers a set of testing functions that are specially designed to the requirements of testing of a web application. These functions are reliable facilitating various options for pos
3 min read
Cloud-based Automation using Selenium in Python and BrowserStack
Selenium is a library that enables the automation of web browsers. It is most popularly used for testing web applications. At the heart of Selenium lies the Selenium WebDriver. WebDriver uses browser automation APIs to control the browser and run tests. This would mean the browser would work in the
6 min read
Install Flask-Sqlalchemy with Pip
Flask is a Python-based web app framework that helps you develop lightweight and deployable web apps. The Flask framework supports several features like URLs, Templating Engines, and Routing. To provide support for database connection and query, one can use the Flask-Sqlalchemy library in Python, wh
3 min read
Python Setup
Let us see how to set up Python in our system. We can directly download the latest version of Python from the official website. Before setting up IDE you need to first install Python in your system, you can refer to this article first for step-by-step procedures. How to install Python on Linux?How t
3 min read
Modulenotfounderror: No Module Named 'httpx' in Python
Python is a versatile and powerful programming language used in various domains. When working with Python, you may encounter the "ModuleNotFoundError: No Module Named 'httpx'" error. This error occurs when the Python interpreter cannot find the required 'httpx' module in its library. In this article
2 min read
Managing Python Dependencies
Managing dependencies becomes crucial to ensure smooth development and deployment processes. In this article, we will explore various methods for managing Python dependencies, from the basics of using pip to more advanced tools like virtualenv and pipenv. How to Manage Dependencies in PythonBelow ar
2 min read
How to install Python in Ubuntu?
This article will guide you through the steps to install Python on Ubuntu, ensuring you're ready to start coding quickly. We will focus on installing Python 3, which is the most widely used version today.Python is typically pre-installed on Ubuntu, particularly Python 3, as it's an essential part of
4 min read
How to Install Scipy In Python on Linux?
In this article, we are going to see how to install Scipy in Python on Linux, SciPy is a python library that is useful in solving many mathematical equations and algorithms, it is a free and open-source Python library built on top of the popular NumPy library. To install Scipy on Linux: There are ge
2 min read