๐Ÿ Python Examples - Comprehensive Code Library
โ† Back to PranavKulkarni.org
Lesson 2 ยท Automation

Web Automation with Selenium

Automate browser tasks, form filling, and UI testing with Selenium.

Why Selenium?

Selenium allows you to automate a real browser (Chrome, Firefox). It's essential for sites that require login, interactions, or heavy JavaScript execution.

Setup

$ pip install selenium webdriver-manager

Basic Browser Control

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

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

driver.get("https://google.com")
print(driver.title)
driver.quit()

Interacting with Elements

from selenium.webdriver.common.by import By

# Finding by name
search_box = driver.find_element(By.NAME, "q")

# Typing and Clicking
search_box.send_keys("Python Examples")
search_box.submit()

Waiting for Elements

Modern sites are dynamic. Always use explicit waits to avoid flaky scripts.

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

wait = WebDriverWait(driver, 10)
elem = wait.until(EC.element_to_be_clickable((By.ID, "login-btn")))

โœ… Practice (30 minutes)

  • Write a script that opens a login page, fills in credentials, and submits.
  • Capture a screenshot of the results page (driver.save_screenshot).
  • Automate a search on a site and print the titles of the results.
  • Run the browser in headless mode (no GUI).