tech beamers
  • Python Lab
    • Python Online Compiler
    • Python Code Checker
    • Python Coding Exercises
    • Python Coding Quizzes
  • SQL Practice
  • SQL Prep
  • Selenium Practice
  • Software Testing
tech beamers
Search
  • Python Lab
    • Python Online Compiler
    • Python Code Checker
    • Python Coding Exercises
    • Python Coding Quizzes
  • SQL Practice
  • SQL Prep
  • Selenium Practice
  • Software Testing
Follow US
© TechBeamers. All Rights Reserved.
Python Tutorials

Selenium 4 Python Tutorial for Beginners

Selenium is open-source, and its API is available in different programming languages such as Java, Python, etc. to perform Web UI Automation testing. Let's explore Selenium Python in this tutorial.
Last updated: Nov 30, 2025 4:28 pm
Meenakshi Agarwal
By Meenakshi Agarwal
No Comments
3 weeks ago
SHARE

Selenium WebDriver is one of the most widely used tools for UI automation testing. Combined with Python’s clean syntax, it becomes a powerful and fast automation stack for web testing, scraping, RPA-style automation, and CI/CD workflows.

Contents
  • Introduction to Selenium Python Automation
    • Installing Python (Updated)
    • Install Selenium WebDriver for Python (Selenium 4)
    • Choose Your Python Editor (IDE)
  • Your First Selenium Python Script (Selenium 4 + Python 3.12)
    • Full Updated Code (Chrome + WebDriver Manager)
    • Understand the Script
  • Cross-Browser Testing With Selenium WebDriver
    • Running Selenium in Headless Mode (Important for CI/CD)
    • Using Explicit Waits (Modern Best Practice)
  • A Better Real-World Example (With Explicit Waits)
  • Recommended Next Steps
  • Python Selenium FAQ

In this updated tutorial, you’ll learn:

  • How to install Python 3.12
  • How to install Selenium 4+
  • Using WebDriver Manager (no manual driver downloads)
  • Writing your first Selenium Python script
  • Using modern locators
  • Running tests on Chrome, Edge, Firefox, and headless browsers
  • Recommended IDEs
  • A full working example + updated code

Introduction to Selenium Python Automation

Web UI automation means interacting with a browser automatically—opening webpages, filling forms, clicking buttons, scraping data, validating UI behaviour, and more. Selenium WebDriver allows automation of:

  • Chrome
  • Edge
  • Firefox
  • Safari
  • Headless Chrome / Firefox
  • Remote drivers (Selenium Grid, cloud services)

Python makes Selenium automation easier due to:

  • Simple syntax
  • Rich ecosystem (pytest, virtualenv, webdriver-manager)
  • Fast learning curve
  • Excellent community support

Installing Python (Updated)

Most Linux and macOS systems already include Python. Windows users should download Python from the official site:

👉 Download Python 3.12+ (Recommended) at: https://www.python.org/downloads/

During installation on Windows:
✔ Check “Add Python to PATH”.

After installation:

python --version

You should see:

Python 3.12.x

Install Selenium WebDriver for Python (Selenium 4)

✔ Install the latest and stable Selenium 4

pip install selenium

✔ Install WebDriver Manager (important)

This automatically downloads the correct browser driver.

pip install webdriver-manager

This means you NO LONGER download chromedriver.exe, geckodriver, or msedgedriver manually.

Choose Your Python Editor (IDE)

Recommended IDEs for Selenium + Python:

PyCharm (Best for most users)

  • Free Community Edition available
  • Intelligent code completion
  • Debugging + virtualenv support

VS Code (Fast and lightweight)

  • Python extension
  • Debugging
  • Integrated terminal

PyDev (Eclipse)

Older but still works for enterprise users.

Your First Selenium Python Script (Selenium 4 + Python 3.12)

This script automates:

  • Opening Google
  • Typing a search query
  • Extracting the top 10 results

Full Updated Code (Chrome + WebDriver Manager)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager  # Fixed: Added import
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Initialize Chrome with WebDriver Manager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.maximize_window()
driver.get("https://www.google.com")

wait = WebDriverWait(driver, 10)  # Modern wait setup

# Accept cookies if popup appears (more robust XPath for 2025 Google)
try:
    accept_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Accept') or contains(text(), 'Agree')]")))
    accept_btn.click()
except:
    pass  # No popup? Proceed

# Search box (stable NAME locator)
search_field = wait.until(EC.visibility_of_element_located((By.NAME, "q")))
search_field.send_keys("Selenium WebDriver Python Tutorial 2025")
search_field.send_keys(Keys.RETURN)

# Wait for results (no sleep!)
results = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "h3")))

print(f"\nTop {min(10, len(results))} Results:\n")
if results:
    for i, r in enumerate(results[:10], 1):
        print(f"{i}: {r.text}")
else:
    print("No results found—try a different query!")

driver.quit()

Understand the Script

Our code runs multiple steps. It starts with the following:

Step 1 → Import libraries

from selenium.webdriver.common.by import By

Selenium 4 removed methods like find_element_by_id. You must use By.

Step 2 → Start Chrome using WebDriver Manager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

No need to download chromedriver manually.

Step 3 → Navigate to a website

driver.get("https://www.google.com")

Step 4 → Locate elements

Chrome’s selector for search box is:

search_field = wait.until(EC.visibility_of_element_located((By.NAME, "q")))

Step 5 → Extract results using CSS Selectors

results = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "h3")))

Cross-Browser Testing With Selenium WebDriver

Selenium Webdriver supports cross-browser testing using Python. It means you can automate other browsers like Edge, Google Chrome, Safari, and headless browsers like Headless Chrome and Headless Firefox.

✔ Firefox (GeckoDriver)

from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))

✔ Microsoft Edge

from webdriver_manager.microsoft import EdgeChromiumDriverManager

driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()))

Running Selenium in Headless Mode (Important for CI/CD)

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = Options()
options.add_argument("--headless=new")
options.add_argument("--window-size=1920,1080")

driver = webdriver.Chrome(
    service=Service(ChromeDriverManager().install()),
    options=options
)

Using Explicit Waits (Modern Best Practice)

Never use time.sleep() in real projects.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
search_box = wait.until(EC.visibility_of_element_located((By.NAME, "q")))

A Better Real-World Example (With Explicit Waits)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.maximize_window()

driver.get("https://www.google.com")

wait = WebDriverWait(driver, 10)

search = wait.until(EC.visibility_of_element_located((By.NAME, "q")))
search.send_keys("Python Selenium Automation 2025")
search.send_keys(Keys.RETURN)

results = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "h3")))

for r in results[:10]:
    print(r.text)

driver.quit()

Recommended Next Steps

To build a full Selenium Python automation framework, learn:

✔ Pytest + Selenium
✔ Page Object Model (POM)
✔ Screenshot capture
✔ Logging
✔ Allure Reports
✔ Parallel execution
✔ Docker + Selenium Grid
✔ CI/CD integration (GitHub Actions, Jenkins, GitLab CI)

Python Selenium FAQ

Do I still need chromedriver in 2025? No. Selenium Manager downloads and manages it automatically.

Is webdriver-manager still needed? Only if you’re on Selenium <4.21 or need specific driver versions.

Which is faster – Selenium or Playwright? Playwright is faster for new projects. Selenium wins for legacy browser support and huge ecosystem.

Lastly, our site needs your support to remain free. Share this post on social media (LinkedIn/Twitter) if you gained some knowledge from this tutorial.

Happy Testing!

Related

TAGGED:python selenium tutorialSelenium Testing in Linux
Share This Article
Whatsapp Whatsapp LinkedIn Reddit Copy Link
Meenakshi Agarwal Avatar
ByMeenakshi Agarwal
Follow:
I’m Meenakshi Agarwal, founder of TechBeamers.com and ex-Tech Lead at Aricent (10+ years). I built the Python online compiler, code checker, Selenium labs, SQL quizzes, and tutorials to help students and working professionals.
Previous Article Python Strings Tutorial Python String Functions
Next Article Chrome Python Shell Extensions Top 5 Python Shell Chrome Extensions for You
Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular This Month

  • → Python Online Compiler
  • → Python Code Checker
  • → Free Python Tutorial
  • → SQL Practice Queries
  • → Code to Flowchart Tool
  • → Python Syntax Guide
  • → Python List & Dict Questions
  • → Selenium Practice Test Page

RELATED TUTORIALS

100+ PYTHON Interview Questions with Answers (PDF Available)

100+ Python Interview Questions and Answers (PDF, 2025)

By Harsh S.
3 weeks ago
Python code optimization tips and tricks to write better programs.

Python: 12 Code Optimization Tips

By Meenakshi Agarwal
3 weeks ago
Sort List of Lists in Python Explained With Examples

How to Sort List of Lists in Python

By Meenakshi Agarwal
3 weeks ago
How to Merge Dictionaries in Python

Python Merge Dictionaries

By Meenakshi Agarwal
3 weeks ago
© TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
Advertisement