Selenium with Python Tutorial: Step-by-Step for Complete Beginners
Browser automation may seem difficult when you are just starting.
You may not understand WebDriver, locators, browser drivers, waits, or testing frameworks. These terms can make Selenium appear more complicated than it really is.
The good news is that you can start with a few simple commands.
This selenium python tutorial beginners guide will teach you everything step by step. You will learn how to install Python and Selenium, open a browser, locate website elements, enter text, click buttons, verify results, and run automated tests.
You do not need previous automation testing experience.
Basic Python knowledge will help, but every example is explained in simple language.
What Is Selenium?
Selenium is an open-source browser automation project.
It allows you to control a web browser through code. Your Selenium script can open a website and complete actions like a real user.
For example, Selenium can:
Open Chrome, Firefox, Edge, or Safari
Visit a website
Enter text into a form
Click buttons and links
Select dropdown options
Read text from a page
Upload files
Switch between browser windows
Take screenshots
Confirm whether a feature works
Selenium WebDriver controls browsers using browser automation APIs. It can run locally on your computer or remotely through a Selenium server.
Selenium is mainly used for web application testing. It does not directly automate normal desktop software.
You can read the official Selenium WebDriver documentation to explore its complete features.
Why Learn Selenium With Python?
Python is a popular choice for beginners because its code is clean and readable.
You can create a basic browser automation script using only a few lines. This lets you focus on testing concepts instead of difficult programming syntax.
Selenium with Python offers several benefits:
Easy-to-read automation scripts
Support for major web browsers
Integration with PyTest
Reusable test functions
Support for automation frameworks
Cross-browser testing
Data-driven testing
Integration with CI/CD tools
Strong practical use in software testing
The current Selenium Python package supports Python 3.10 and newer versions. Modern Selenium versions can also manage browser drivers through Selenium Manager.
This means beginners normally do not need to download ChromeDriver manually.
Students who want instructor-led practical learning can explore the Selenium with Python Training in Bangalore from Fast Learning Technologies.
What You Will Learn in This Tutorial
This selenium python tutorial beginners guide covers:
Installing Python
Creating a project folder
Setting up a virtual environment
Installing Selenium
Opening Chrome with Python
Visiting a website
Finding website elements
Entering text
Clicking buttons
Adding test validations
Using explicit waits
Taking screenshots
Running tests with PyTest
Solving common Selenium errors
Let us begin with the required setup.
Step 1: Install Python
First, install Python on your computer.
Visit the official Python downloads page and download a supported Python version.
During installation on Windows, select the option:
Add Python to PATH
This option lets you run Python commands from Command Prompt.
After installation, open your terminal and enter:
python --version
Windows users can also try:
py --version
Mac and Linux users may need:
python3 --version
You should see a Python version number.
For example:
Python 3.x.x
This confirms that Python is installed correctly.
When the command is not recognised, restart your terminal. You may need to reinstall Python and select the PATH option.
Beginners who need to improve their coding knowledge can also review the Python Training Course in Bangalore.
Step 2: Install a Code Editor
You need a code editor to write Python scripts.
Common options include:
Visual Studio Code
PyCharm
Sublime Text
IDLE
Visual Studio Code is a simple choice for beginners.
Install the Python extension inside Visual Studio Code. It provides syntax highlighting, error checking, code suggestions, and terminal support.
You can also use PyCharm Community Edition.
The code examples in this tutorial will work in any Python-compatible editor.
Step 3: Create a Selenium Project Folder
Create a new folder for your project.
Name it:
selenium-python-beginner
You can create the folder manually or use the terminal:
mkdir selenium-python-beginner
cd selenium-python-beginner
Open this folder inside your code editor.
Keeping all files inside one project folder makes your automation code easier to manage.
Step 4: Create a Virtual Environment
A virtual environment keeps project packages separate.
Without a virtual environment, packages installed for one project may affect another project.
Run this command on Windows:
python -m venv venv
Activate the environment:
venv\Scripts\activate
You can use py when the python command does not work:
py -m venv venv
venv\Scripts\activate
On macOS or Linux, use:
python3 -m venv venv
source venv/bin/activate
After activation, you should see (venv) in the terminal.
For example:
(venv) C:\selenium-python-beginner>
A virtual environment is not required for every small script. However, it is a recommended practice for organised projects.
Step 5: Install Selenium
Now install the Selenium Python package.
Run:
pip install -U selenium
You can check the installed package using:
pip show selenium
The terminal should display information such as the package name, version, and installation location.
You can also install PyTest now:
pip install -U pytest
PyTest helps you create structured automated tests.
The official PyTest documentation also recommends installing it through pip.
Step 6: Write Your First Selenium Python Script
Create a new file named:
first_selenium_test.py
Add the following code:
from selenium import webdriver
driver = webdriver.Chrome()
try:
driver.get("https://www.selenium.dev/")
print("Page title:", driver.title)
finally:
driver.quit()
Save the file.
Run it from the terminal:
python first_selenium_test.py
Mac and Linux users may need:
python3 first_selenium_test.py
Chrome should open automatically.
The browser will visit the Selenium website. Your terminal will display the page title. The browser will then close.
Congratulations. You have completed your first browser automation script.
Understanding Your First Selenium Script
Let us understand each line.
Import WebDriver
from selenium import webdriver
This line imports Selenium WebDriver.
WebDriver provides the commands required to control a browser.
Open Chrome
driver = webdriver.Chrome()
This command starts a Chrome browser session.
Modern Selenium uses Selenium Manager to locate or manage the required driver in normal setups.
Visit a Website
driver.get("https://www.selenium.dev/")
The get() method opens the provided web address.
Read the Page Title
print("Page title:", driver.title)
The title property reads the title of the current page.
Close the Browser
driver.quit()
The quit() method closes all browser windows created during the session.
The finally block ensures that the browser closes even when the test produces an error.
Step 7: Understand Selenium Locators
Your script must identify an element before interacting with it.
A locator tells Selenium which website element you want to use.
For example, your script must find a username field before entering a username.
Selenium supports eight traditional locator strategies. These include ID, name, class name, CSS selector, XPath, link text, partial link text, and tag name.
Import the By class:
from selenium.webdriver.common.by import By
Here are common examples:
driver.find_element(By.ID, "username")
driver.find_element(By.NAME, "email")
driver.find_element(By.CLASS_NAME, "login-field")
driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
driver.find_element(By.XPATH, "//button[text()='Login']")
driver.find_element(By.LINK_TEXT, "Register")
driver.find_element(By.PARTIAL_LINK_TEXT, "Forgot")
driver.find_element(By.TAG_NAME, "h1")
When a unique and stable ID is available, it is generally a good locator choice. The official Selenium test-practices guide recommends unique and predictable IDs when possible.
Avoid long and fragile XPath expressions.
A locator should remain stable when small website layout changes happen.
Step 8: Automate a Sample Web Form
Selenium provides a sample form for learning.
Replace your earlier code with this example:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get(
"https://www.selenium.dev/selenium/web/web-form.html"
)
text_box = driver.find_element(By.NAME, "my-text")
submit_button = driver.find_element(
By.CSS_SELECTOR,
"button"
)
text_box.send_keys("Learning Selenium with Python")
submit_button.click()
message = driver.find_element(By.ID, "message")
print("Result:", message.text)
finally:
driver.quit()
Run the file again:
python first_selenium_test.py
The script will:
Open Chrome
Visit the Selenium sample form
Find the text field
Enter a message
Find the submit button
Click the button
Read the confirmation message
Close Chrome
The official Selenium beginner documentation uses the same sample web form for its first-script examples.
Step 9: Enter and Clear Text
Use send_keys() to enter text:
text_box.send_keys("Selenium Python")
Use clear() to remove existing text:
text_box.clear()
You can combine both commands:
text_box.clear()
text_box.send_keys("New test data")
This is useful when an input field already contains a default value.
Step 10: Click a Button
Use the click() method:
submit_button.click()
Selenium can click:
Buttons
Links
Checkboxes
Radio buttons
Menu items
Icons
Other clickable elements
Before clicking, confirm that the element is visible and enabled.
Dynamic websites may need an explicit wait before Selenium can click the element.
Step 11: Add an Assertion
A script that only clicks buttons is not a complete automated test.
Your test must confirm whether the expected result appeared.
Python’s assert statement can perform this validation:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get(
"https://www.selenium.dev/selenium/web/web-form.html"
)
driver.find_element(
By.NAME,
"my-text"
).send_keys("Beginner Selenium Test")
driver.find_element(
By.CSS_SELECTOR,
"button"
).click()
message = driver.find_element(By.ID, "message")
assert message.text == "Received!"
print("Test passed successfully")
finally:
driver.quit()
The test passes when the actual message equals Received!.
The test fails when the message is different.
Assertions turn normal Selenium scripts into useful test cases.
This is one of the most important lessons in any selenium python tutorial beginners course.
Step 12: Use Explicit Waits
Modern websites often load content dynamically.
A button may appear after an API response. A login form may become visible after an animation. Selenium may search for the element before it is ready.
Beginners often use:
import time
time.sleep(5)
This pauses the script for exactly five seconds.
However, fixed sleeps can make tests slow and unreliable.
An explicit wait is usually better.
It waits for a specific condition and continues as soon as that condition becomes true.
Use this example:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
try:
driver.get(
"https://www.selenium.dev/selenium/web/web-form.html"
)
wait = WebDriverWait(driver, 10)
text_box = wait.until(
EC.visibility_of_element_located(
(By.NAME, "my-text")
)
)
text_box.send_keys("Testing with explicit waits")
submit_button = wait.until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, "button")
)
)
submit_button.click()
message = wait.until(
EC.visibility_of_element_located(
(By.ID, "message")
)
)
assert message.text == "Received!"
finally:
driver.quit()
The script waits for up to ten seconds.
When the element becomes ready in two seconds, the script continues immediately. It does not wait for the full ten seconds.
Step 13: Take a Screenshot
Screenshots help you record test results and investigate failures.
Use:
driver.save_screenshot("test-result.png")
Add it after your validation:
message = driver.find_element(By.ID, "message")
assert message.text == "Received!"
driver.save_screenshot("successful-test.png")
The screenshot will be saved inside your project folder.
In a real automation framework, you can configure screenshots to run automatically after test failures.
Step 14: Run Selenium With PyTest
PyTest makes tests easier to organise and execute.
Create a new file:
test_web_form.py
PyTest automatically discovers test files that follow naming patterns such as test_*.py or *_test.py.
Add this code:
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
@pytest.fixture
def driver():
browser = webdriver.Chrome()
browser.maximize_window()
yield browser
browser.quit()
def test_form_submission(driver):
driver.get(
"https://www.selenium.dev/selenium/web/web-form.html"
)
wait = WebDriverWait(driver, 10)
text_box = wait.until(
EC.visibility_of_element_located(
(By.NAME, "my-text")
)
)
text_box.send_keys("Selenium with PyTest")
wait.until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, "button")
)
).click()
message = wait.until(
EC.visibility_of_element_located(
(By.ID, "message")
)
)
assert message.text == "Received!"
Run the test:
pytest -v
PyTest should display a passed test result.
Understanding the PyTest Fixture
This section creates a reusable browser setup:
@pytest.fixture
def driver():
browser = webdriver.Chrome()
yield browser
browser.quit()
The browser opens before the test begins.
The yield statement provides the browser to the test.
After the test finishes, PyTest runs browser.quit().
This approach prevents you from repeating browser setup and closing code in every test.
Common Selenium Python Errors
Every selenium python tutorial beginners guide should explain common errors.
ModuleNotFoundError
You may see:
ModuleNotFoundError: No module named 'selenium'
Install Selenium:
pip install -U selenium
Also confirm that your virtual environment is active.
NoSuchElementException
This error means Selenium could not locate the element.
Possible reasons include:
The locator is incorrect
The page has not loaded
The element is inside an iframe
The element is inside another window
The element appears dynamically
The element does not exist
Check the locator using browser developer tools.
Use an explicit wait when the element takes time to appear.
TimeoutException
A timeout happens when an expected condition does not become true within the given time.
Check:
The locator
The internet connection
The page state
The expected condition
The waiting duration
Increasing the waiting time will not fix an incorrect locator.
ElementClickInterceptedException
This error appears when another element blocks the element you want to click.
Common causes include:
Cookie notices
Pop-ups
Loading overlays
Sticky menus
Animations
Wait until the element is clickable. You may also need to close the blocking pop-up.
Browser Opens and Closes Immediately
The browser closes when your script finishes.
It may also close because an error stopped the script.
Run the file through the terminal and read the complete error message.
Do not add long sleep commands only to keep Chrome open. Use proper debugging methods instead.
Selenium Python Best Practices
Follow these practices while learning:
Use meaningful test names
Keep each test focused on one purpose
Select stable locators
Prefer unique IDs when available
Avoid long XPath expressions
Add assertions to your tests
Use explicit waits
Avoid unnecessary fixed sleeps
Close the browser after every test
Keep tests independent
Store test data separately
Capture screenshots after failures
Use Git for version control
Review error messages carefully
Understand a feature manually before automating it
Good automation is not only about writing code.
You must understand the application, business requirements, test risks, and expected customer behaviour.
Students who need these testing fundamentals can start with Manual Testing Training in Bangalore.
Selenium Python Learning Roadmap
Follow this roadmap after completing the basic tutorial.
Stage 1: Learn Python Fundamentals
Study:
Variables
Data types
Strings
Lists
Dictionaries
Conditions
Loops
Functions
Classes
Exceptions
Files
Modules
Stage 2: Learn Manual Testing
Understand:
SDLC
STLC
Test scenarios
Test cases
Defect reports
Regression testing
Agile
Scrum
Jira
Stage 3: Learn Selenium WebDriver
Practise:
Browser commands
Locators
Web elements
Waits
Dropdowns
Alerts
Frames
Windows
File uploads
Screenshots
Stage 4: Learn PyTest
Focus on:
Test discovery
Fixtures
Assertions
Markers
Parametrisation
Setup and teardown
Test reports
Stage 5: Build an Automation Framework
Learn:
Page Object Model
Reusable utilities
Configuration files
Logging
Test data management
Screenshot handling
HTML reports
Stage 6: Learn Git and CI/CD
Store your code in GitHub.
Then learn how to run Selenium tests through Jenkins or GitHub Actions.
You can explore the complete Software Testing Master Course for manual testing, Selenium, API testing, Playwright, performance testing, and CI/CD concepts.
Final Thoughts
Selenium becomes easier when you learn through small working scripts.
Do not start by building a complicated framework.
First, learn how to:
Open a browser
Visit a page
Find an element
Perform an action
Verify the result
Close the browser
The best selenium python tutorial beginners learning path is:
Python basics → manual testing → Selenium locators → waits → assertions → PyTest → automation frameworks
Run every code example yourself.
Change the test data. Try different locators. Add another assertion. Intentionally create an error and study the result.
Regular practice will improve your automation skills faster than reading theory alone.
Once you understand the basics, build a complete project and upload it to GitHub. A practical automation project can help demonstrate your knowledge during interviews.
Frequently Asked Questions
1. Is Selenium with Python easy for complete beginners?
Yes. Python has readable syntax, which makes Selenium scripts easier to understand. Learning basic Python and manual testing first will make your progress faster.
2. Do I need to install ChromeDriver manually?
Modern Selenium versions normally manage browser drivers through Selenium Manager. Manual setup may still be needed in restricted networks or unusual environments.
3. Can I learn Selenium without knowing Python?
You should learn basic Python before starting advanced Selenium topics. Focus on variables, conditions, loops, functions, classes, and exception handling.
4. Is Selenium used only for testing?
Selenium is primarily designed for browser automation. Software testing is its most common use because it can automate user actions and verify web application behaviour.
5. Which is better for Selenium Python: PyTest or unittest?
Both can work. PyTest is often easier for beginners because it provides simple assertions, fixtures, test discovery, parametrisation, and plugin support.
6. How long does it take to learn Selenium with Python?
The time depends on your Python knowledge and practice schedule. Focus on practical skills rather than trying to complete every topic quickly.
7. What should I learn after this selenium python tutorial beginners guide?
Learn dropdowns, alerts, frames, multiple windows, file uploads, Page Object Model, PyTest fixtures, data-driven testing, reporting, Git, and CI/CD.
8. Is Selenium enough to get an automation testing job?
Selenium is an important skill, but employers may also expect manual testing, Python, SQL, API testing, Git, PyTest, framework development, and project experience.