keepcalculator

06 Numbers and Powers

Selenium WebDriver Python Tutorial with Examples

Card No. 06·41194 · Updated · Standard formula, shown below

This tutorial explains how to use Selenium WebDriver with Python to automate web browsers, covering installation, locating elements, interacting with pages, and running real test examples. You’ll write scripts that open a browser, click buttons, fill forms, and verify results, all in plain Python code. By the end, you’ll have a working setup and several copy-paste-ready examples to build on.

What Is Selenium WebDriver and Why Python?

Selenium WebDriver is a browser automation library that lets you control a real browser (Chrome, Firefox, Edge, Safari) programmatically. It’s the industry standard for end-to-end web testing because it drives the actual browser, so you see exactly what a user sees. Python is the most popular language for Selenium because its syntax is short, the official bindings are well maintained, and the ecosystem of testing tools (pytest, unittest) integrates cleanly.

When people search for a “Selenium webdriver tutorial with examples python,” they usually want to automate a repetitive task or write automated tests for a website. The same library handles both: you can scrape data, test login flows, or fill out forms hundreds of times without touching the browser manually.

Setting Up Selenium with Python (Step by Step)

Before writing any code, you need three things: Python installed (3.7 or newer), the Selenium package, and a browser driver. The driver is a small executable that Selenium uses to talk to your specific browser. Modern Selenium (4.6+) includes Selenium Manager, which automatically downloads the correct driver for Chrome, Firefox, or Edge when you run a script — you no longer need to manually download chromedriver for most cases.

  1. Install Python from python.org or your system package manager. Verify with python --version.
  2. Install Selenium using pip: pip install selenium
  3. Install a browser you already have (Chrome, Firefox, Edge) — Selenium will use it as-is.
  4. Optional: If you’re on an older Selenium version or need a specific driver, install it manually and add it to your PATH.

Here’s the simplest possible script that opens a browser and visits a page:

from selenium import webdriver

driver = webdriver.Chrome()  # Selenium Manager handles the driver
driver.get("https://example.com")
print(driver.title)
driver.quit()

That’s it. If you run this and a Chrome window opens, your setup works. The driver.quit() line is essential — it closes the browser and frees memory.

Finding Elements: The Core Skill

Every Selenium script revolves around locating elements on a page. You can find elements by ID, class name, CSS selector, XPath, link text, or tag name. The two most common methods are find_element(By.ID, "value") for a single element and find_elements(By.CLASS_NAME, "value") for a list of them.

Here’s a practical example that finds a search box, types text, and submits:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://www.google.com")
search_box = driver.find_element(By.NAME, "q")
search_box.send_keys("selenium python")
search_box.send_keys(Keys.RETURN)

Notice the imports: By for the locator strategy and Keys for special keyboard input. For more complex pages, XPath is the most flexible locator. For example, //button[contains(text(),'Submit')] finds a button whose text contains “Submit”. But prefer simple locators (ID, name) when they exist — they’re faster and less brittle.

Interacting with Pages: Clicks, Forms, and Waits

Once you have an element, you can call methods on it: .click(), .send_keys(), .clear(), .text, .get_attribute(). For dropdowns, use the Select class. For checkboxes, just click them. The tricky part is timing: modern sites load content dynamically, so you must wait for elements to appear.

Never use time.sleep() as your primary wait strategy — it’s slow and flaky. Instead, use explicit waits:

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

wait = WebDriverWait(driver, 10)
button = wait.until(EC.element_to_be_clickable((By.ID, "submit")))
button.click()

This waits up to 10 seconds for the button to become clickable, then clicks it. Explicit waits are the single best practice for reliable Selenium scripts. A full login example looks like this:

driver.get("https://example.com/login")
driver.find_element(By.ID, "username").send_keys("testuser")
driver.find_element(By.ID, "password").send_keys("testpass")
driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click()
wait.until(EC.url_contains("/dashboard"))
print("Login successful")

Complete Example: Automating a Search and Scraping Results

Let’s put it all together. This script searches a book store (using the public demo site books.toscrape.com) and prints the titles of the first five books:

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()
wait = WebDriverWait(driver, 10)

try:
    driver.get("https://books.toscrape.com/")
    # Wait for the book grid to load
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "article.product_pod")))
    books = driver.find_elements(By.CSS_SELECTOR, "article.product_pod h3 a")
    for book in books[:5]:
        print(book.text)
finally:
    driver.quit()

This example shows the pattern: navigate, wait for the right condition, locate elements, extract data, and always clean up in a finally block so the browser closes even if an error occurs.

Common Problems and How to Fix Them

Beginners hit the same few issues repeatedly. Here are the most common ones and their fixes:

  • Element not found: The page hasn’t loaded the element yet. Use an explicit wait instead of immediate find_element.
  • Element not clickable: Something overlays it (a modal, a cookie banner). Wait for the overlay to disappear, or use driver.execute_script("arguments[0].click()", element) to force a click.
  • Stale element reference: The page changed after you found the element. Re-find the element right before you use it.
  • Driver version mismatch: Update Selenium to 4.6+ and let Selenium Manager handle the driver. If you still see errors, manually download the matching driver.
  • Headless mode issues: Some sites block headless browsers. Try running with a visible window first, then switch to headless after the script works.

Selenium vs. Other Tools: When to Use What

Selenium is not the only browser automation tool. Playwright and Puppeteer are modern alternatives that are faster and have better built-in waiting. But Selenium remains the most widely supported, works with every major browser, and has the largest community. Choose Selenium when you need cross-browser compatibility or are working in an existing Selenium test suite. Choose Playwright if you’re starting fresh and want built-in auto-waits and better debugging. For simple HTTP requests without JavaScript, use requests + BeautifulSoup — much lighter and faster, but it won’t work on JavaScript-heavy sites.

Best Practices for Reliable Scripts

Writing a script that works once is easy; writing one that works every time takes discipline. Follow these rules:

  • Always use explicit waits, never fixed sleeps.
  • Use unique, stable locators (ID, name, data-testid) instead of fragile XPaths.
  • Wrap your code in try/finally to ensure driver.quit() runs.
  • Run tests in headless mode for CI: options.add_argument("--headless").
  • Keep test data separate from code — don’t hardcode passwords.
  • Use Page Object Model for large test suites to avoid duplicating locators.

Frequently Asked Questions

Is Selenium free to use?

Yes, Selenium is open source (Apache 2.0 license) and completely free. You only pay for the browser and the time you spend writing scripts.

Can I use Selenium to scrape any website?

Technically yes, but you must respect the website’s terms of service and robots.txt. Many sites block automated traffic, so use Selenium responsibly — throttle requests and don’t overload servers.

What’s the difference between find_element and find_elements?

find_element returns the first matching element and raises an error if none exist. find_elements returns a list (possibly empty) of all matching elements. Use the plural version when you expect multiple items or want to avoid exceptions.

Do I need to learn XPath for Selenium?

Not strictly, but it’s extremely useful. XPath handles complex queries that CSS can’t, like finding an element based on its text content or its position relative to another element. Learn the basics — it will save you hours.

How do I run Selenium scripts on a schedule?

Use a task scheduler (cron on Linux, Task Scheduler on Windows) or a CI tool like Jenkins or GitHub Actions. Just make sure the browser and driver are installed on the machine that runs the script.

Why does my script work in one browser but not another?

Browsers render pages slightly differently, and locators may behave differently. Test on all target browsers and use explicit waits to handle timing differences. Also ensure you’re using the correct driver for each browser.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →