How to Install Selenium for Python Step-by-Step
How to Install Selenium for Python: Step-by-Step Guide
๐งช Introduction
Selenium is one of the most powerful and widely used automation tools for testing web applications. It's open-source, supports multiple programming languages—including Python—and enables automated browser interaction.
This article provides a step-by-step guide to installing Selenium with Python on your system in 2025.
๐ What Is Selenium?
Selenium is a suite of tools that allows you to automate browsers. It supports:
Cross-browser testing (Chrome, Firefox, Safari, Edge)
Web scraping (in some cases)
Regression and functional testing
Integration with test frameworks like PyTest and Unittest
✅ Prerequisites
Before starting, make sure:
Python is installed on your system
pip (Python package installer) is available
A web browser (like Chrome or Firefox) is installed
Internet connection is active
๐ Step 1: Install Python (If Not Already Installed)
๐ฝ Windows:
Go to the official Python website:
๐ https://www.python.org/downloads/
Download the latest version for Windows
Run the installer
✅ Select “Add Python to PATH”
Click Install Now
๐ฝ macOS:
brew install python
Or download manually from the Python website.
๐ฝ Linux (Ubuntu):
sudo apt update
sudo apt install python3 python3-pip
๐ Step 2: Verify Python Installation
Run the following command in your terminal or command prompt:
python --version
Or
python3 --version
If installed correctly, it should return something like:
nginx
Python 3.11.5
⚙️ Step 3: Install pip (if missing)
Most Python versions come with pip. If not, install it using:
python -m ensurepip --upgrade
Then verify:
pip --version
๐ Step 4: Install Selenium Using pip
Run:
pip install selenium
If you're using Python 3:
pip3 install selenium
You should see something like:
nginx
Successfully installed selenium-4.x.x
๐ Step 5: Verify Selenium Installation
Create a test script:
python
# test_selenium.py
from selenium import webdriver
print("Selenium Installed Successfully!")
Run it using:
python test_selenium.py
If no error shows, Selenium is installed!
๐ Step 6: Download WebDriver
Selenium requires a WebDriver to interact with browsers. Let’s choose ChromeDriver for this tutorial.
➤ For Chrome:
Check your Chrome version:
Open Chrome → Click 3 dots → Help → About Google Chrome
Example: Version 117.0.5938.132
Download the matching ChromeDriver:
๐ https://sites.google.com/chromium.org/driver/
Extract the ZIP and place the chromedriver executable in:
Windows: Inside your project folder or in C:\WebDrivers\
macOS/Linux: /usr/local/bin/
Add to PATH if needed.
๐งช Step 7: Write Your First Selenium Script
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
# Set up ChromeDriver path
service = Service("path/to/chromedriver")
# Launch Chrome browser
driver = webdriver.Chrome(service=service)
# Open a website
driver.get("https://www.google.com")
# Wait for 5 seconds
time.sleep(5)
# Close browser
driver.quit()
Replace "path/to/chromedriver" with your actual path.
๐งฐ Step 8: Automate Simple Tasks
Search something on Google:
from selenium.webdriver.common.keys import Keys
driver.get("https://www.google.com")
search = driver.find_element(By.NAME, "q")
search.send_keys("Selenium with Python")
search.send_keys(Keys.RETURN)
time.sleep(5)
driver.quit()
๐ Step 9: Use Other Browsers (Optional)
Firefox:
Install GeckoDriver from:
๐ https://github.com/mozilla/geckodriver/releases
from selenium.webdriver.firefox.service import Service
driver = webdriver.Firefox(service=Service("path/to/geckodriver"))
Edge:
Install EdgeDriver from:
๐ https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
๐งฉ Step 10: Install a Virtual Environment (Recommended)
Using a virtual environment keeps your dependencies isolated.
pip install virtualenv
virtualenv venv
source venv/bin/activate # macOS/Linux
venv\Scripts\activate # Windows
Then install Selenium inside the environment:
pip install selenium
๐ง Optional Tools and Plugins
WebDriver Manager (auto-handles driver versions)
pip install webdriver-manager
Example:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
PyTest or Unittest for writing test cases
BeautifulSoup for enhanced web scraping
Headless Mode for background execution
๐ Step 11: Troubleshooting
Problem Solution
ChromeDriver not found Check path and add to system PATH
Version mismatch Match ChromeDriver to your browser version
pip install fails Upgrade pip using pip install --upgrade pip
Permission denied Use sudo (Linux/macOS) or Admin CMD
๐ Update Selenium
To upgrade Selenium later:
pip install --upgrade selenium
๐ Conclusion
You’ve now installed Selenium for Python and are ready to automate browser actions!
Selenium + Python is a powerful combo used in:
Web automation
Regression testing
Data scraping (with care!)
CI/CD pipelines
Mastering Selenium sets you up for QA, DevOps, and Automation Engineering roles.
✅ What’s Next?
Learn Locators (ID, Name, XPath, CSS Selector)
Explore Waits (Explicit/Implicit)
Create a framework using PyTest
Run tests in headless mode
Integrate with tools like Jenkins and GitHub Actions
Comments
Post a Comment