Introduction
Before starting browser automation testing, it is important to configure the development environment correctly. Selenium WebDriver with Python allows testers to automate web applications using Python scripts and supported browsers.
However, beginners often face difficulties installing Python, configuring Selenium, selecting a code editor, managing browser drivers and executing their first automation script.
This tutorial explains how to set up a complete Selenium Python environment step by step. By the end, you will be able to launch a browser, open a website and execute a basic automated test.
1. Understand the Tools Required
The following tools are required for a basic Selenium Python setup.
| Tool | Purpose |
|---|---|
| Python | Programming language |
| Selenium WebDriver | Browser automation |
| Google Chrome | Browser under test |
| VS Code or PyCharm | Code editor |
| pip | Python package installation |
| PyTest | Test execution |
| Git | Version control |
You can begin with Python, Selenium and Chrome. Other tools can be introduced as your automation project grows.
2. Install Python
Python is the programming language used to develop Selenium automation scripts.
Visit the official Python website:
https://www.python.org/downloads/
Download a currently supported Python release suitable for your operating system.
During Windows installation, select the option to add Python to PATH if it is available.
After installation, open Command Prompt or Terminal and execute:
python --versionIf the command is not recognised on Windows, try:
py --versionOn macOS or Linux, you may need:
python3 --versionThe terminal should display the installed Python version.
3. Verify pip Installation
Pip is the package installer used to install Python libraries.
Check whether pip is available:
python -m pip --versionOn systems using python3, execute:
python3 -m pip --versionIf pip is unavailable, follow the official Python installation or pip documentation for your operating system.
Upgrade pip when required:
python -m pip install --upgrade pipUsing python -m pip helps ensure that packages are installed for the intended Python interpreter.
4. Install Visual Studio Code
Visual Studio Code is a lightweight code editor suitable for Python automation development.
Download it from:
https://code.visualstudio.com/
After installation:
Open Visual Studio Code.
Navigate to Extensions.
Search for the Python extension.
Install the Microsoft Python extension.
Open your Selenium project folder.
Select the correct Python interpreter.
You can also use PyCharm if you prefer a dedicated Python IDE.
5. Create a Python Virtual Environment
A virtual environment keeps project dependencies separate from other Python projects.
Create a folder named selenium-python-project.
Open its terminal and execute:
python -m venv venvActivate the environment on Windows Command Prompt:
venv\Scripts\activateFor Windows PowerShell:
.\venv\Scripts\Activate.ps1For macOS or Linux:
source venv/bin/activateOnce activated, the terminal generally displays the environment name.
Virtual environments help avoid dependency conflicts when working on multiple automation projects.
6. Install Selenium WebDriver
With the virtual environment activated, install Selenium:
python -m pip install seleniumVerify the installation:
python -m pip show seleniumYou should see the installed Selenium package details.
Selenium provides the WebDriver API required to interact with supported browsers.
7. Install Google Chrome
Download Google Chrome from:
https://www.google.com/chrome/
Install the browser and verify that it opens successfully.
You should also check the installed browser version.
Selenium needs a compatible browser and browser automation driver to establish a WebDriver session.
Do You Need to Download ChromeDriver Separately?
In many current Selenium setups, you do not need to download ChromeDriver manually.
Selenium Manager, included with modern Selenium releases, can discover, download and manage compatible drivers automatically.
Manual driver configuration may still be required in restricted corporate environments, offline systems or specialised configurations.
Avoid mixing an incompatible browser and driver version.
8. Create Your First Selenium Python Program
Create a file named first_selenium_test.py.
Add the following code:
from selenium import webdriver
# Launch Chrome browser
driver = webdriver.Chrome()
# Maximise browser window
driver.maximize_window()
# Open a website
driver.get("https://www.google.com")
# Print page title
print("Page Title:", driver.title)
# Print current URL
print("Current URL:", driver.current_url)
# Close browser
driver.quit()Save the file and execute:
python first_selenium_test.pyIf the configuration is correct, Chrome should launch, open Google and close after the script finishes.
The terminal should display the page title and URL.
9. Understand the Selenium WebDriver Code
Let us understand the important statements.
Import WebDriver
from selenium import webdriverThis imports the Selenium WebDriver module.
Create a browser session
driver = webdriver.Chrome()This starts a Chrome WebDriver session.
Navigate to a website
driver.get("https://www.google.com")This opens the specified URL.
Read the page title
print(driver.title)This returns the title of the current browser page.
Close the session
driver.quit()This closes the browser session and releases associated resources.
10. Install and Configure PyTest
PyTest is widely used for executing Python automation tests.
Install it:
python -m pip install pytestCreate a file named test_browser.py.
from selenium import webdriver
def test_google_title():
driver = webdriver.Chrome()
try:
driver.get("https://www.google.com")
assert "Google" in driver.title
finally:
driver.quit()Run the test:
pytest -vPyTest discovers the test function and executes it.
The assertion verifies that the page title contains the expected text.
11. Install Additional Python Testing Libraries
As your automation framework develops, you may need additional packages.
Some commonly used packages include:
| Package | Purpose |
|---|---|
| selenium | Browser automation |
| pytest | Test execution |
| pytest-html | HTML test reports |
| pytest-xdist | Parallel test execution |
| openpyxl | Excel data handling |
| requests | API communication |
| python-dotenv | Environment configuration |
Install selected packages:
python -m pip install pytest-html pytest-xdist openpyxl requests python-dotenvDo not install every package unnecessarily. Add dependencies according to your project requirements.
12. Save Project Dependencies in requirements.txt
A requirements file helps reproduce the project environment.
Create requirements.txt:
selenium
pytest
pytest-html
pytest-xdist
openpyxl
requests
python-dotenvInstall the dependencies on another environment using:
python -m pip install -r requirements.txtFor more controlled project deployments, pin tested package versions in the requirements file rather than leaving all dependencies unpinned.
13. Recommended Selenium Python Project Structure
As you move beyond beginner scripts, organise your project into separate folders.
selenium-python-project
│
├── venv
│
├── pages
│ ├── login_page.py
│ └── home_page.py
│
├── tests
│ ├── test_login.py
│ └── test_home.py
│
├── utilities
│ ├── config_reader.py
│ └── screenshot.py
│
├── reports
│
├── conftest.py
├── requirements.txt
└── pytest.iniThis structure supports reusable page classes, test configuration, utilities and reporting.
You can later introduce Page Object Model, fixtures, data-driven testing and logging.
14. Common Selenium Python Installation Problems
Problem 1: Python command not recognised
Possible reason: Python is not installed correctly or its executable directory is not available in PATH.
Solution: Verify the installation and configure PATH, or use the appropriate py or python3 command.
Problem 2: No module named selenium
Possible reason: Selenium is installed in a different Python environment.
Solution:
python -m pip install seleniumCheck that the intended virtual environment is activated.
Problem 3: ChromeDriver compatibility error
Possible reason: The configured browser driver is incompatible with the browser.
Solution: Use a current Selenium release and Selenium Manager where possible. Review proxy, network and browser restrictions if driver management fails.
Problem 4: Browser does not launch
Possible reason: Browser installation, driver configuration, permissions or environment settings may be incorrect.
Solution: Check the browser installation, Selenium version, driver logs and system permissions.
Problem 5: PyTest does not discover tests
Possible reason: The file or function does not follow PyTest discovery conventions.
Solution: Use filenames beginning with test_ and test functions beginning with test_.
15. What to Learn After Environment Setup
Once the environment is ready, follow a structured learning sequence:
Python variables, data types and functions.
Python classes and object-oriented programming.
Selenium WebDriver commands.
Selenium locators: XPath and CSS selectors.
Browser windows, frames, alerts and dropdowns.
Implicit and explicit waits.
PyTest fixtures and assertions.
Page Object Model.
Data-driven automation.
Framework development and reporting.
Git, Jenkins and CI/CD integration.
Real-time automation testing projects.
This progression will help you move from basic browser scripts to a maintainable automation framework.
Selenium Python Training at Palium Skills
Palium Skills offers practical Automation Testing training for freshers, manual testers and working professionals who want to develop skills in web automation.
The Selenium Python learning programme can include:
Python programming fundamentals.
Selenium installation and environment setup.
WebDriver commands and browser handling.
XPath and CSS selectors.
Dynamic elements and waits.
PyTest framework.
Page Object Model.
Data-driven testing.
Automation framework development.
Reporting and debugging.
Practical project implementation.
Git and CI/CD fundamentals.
Training options include classroom sessions at South Kolkata and Salt Lake centres, along with instructor-led live online sessions for learners across India.
Contact Palium Skills
Website: www.paliumskills.com
Call: 8420594969 / 7044871915
WhatsApp: 9903130500
Email: info@paliumskills.com
South Kolkata Centre: 1st Floor, Sheeba Bhavan, 1/22 Poddar Nagar, Near South City Mall, Kolkata – 700068.
Salt Lake Centre: 5th Floor, RDB Boulevard, Block GP, Salt Lake Electronic Complex, Kolkata – 700091.
Enquire about Selenium with Python, Selenium with Java and Playwright Automation Testing training.
Frequently Asked Questions (FAQs)
1. What is required to install Selenium with Python?
You need Python, pip, Selenium WebDriver, a supported browser and a code editor. PyTest can be added for structured test execution.
2. Do I need to install ChromeDriver manually?
Not necessarily. Modern Selenium releases include Selenium Manager, which can manage compatible browser drivers automatically in many environments.
3. Which IDE is suitable for Selenium Python?
Visual Studio Code and PyCharm are both suitable for developing Selenium Python automation scripts.
4. Is a virtual environment necessary?
It is not compulsory, but it is strongly recommended because it isolates project dependencies and reduces package conflicts.
5. How can I execute Selenium Python scripts?
You can execute a Python script using python filename.py or run PyTest test cases using pytest -v.
6. Can Selenium Python run on Windows and Linux?
Yes. Selenium Python supports major operating systems, subject to browser and driver compatibility.
7. What should I learn after installing Selenium?
Start with WebDriver commands, locators, waits, PyTest, Page Object Model, data-driven testing and framework development.
8. Is Selenium Python useful for automation testing jobs?
Yes. Selenium Python is a valuable skill for web automation roles, particularly when combined with Python programming, testing fundamentals, API testing and CI/CD knowledge.
Conclusion
Setting up Selenium with Python is the foundation of browser automation testing. Once Python, Selenium, a browser and the testing environment are configured correctly, you can begin writing scripts and developing automated test cases.
Start with simple browser navigation and gradually learn locators, waits, PyTest and framework development. Consistent practical exercises will help you build confidence and prepare for real-world automation projects.
No comments:
Post a Comment