Introduction to Selenium
Selenium is one of the most widely used open-source tools for automating web browsers, letting test scripts click, type, and read content just as a real user would.
What Selenium Does
Selenium WebDriver controls a real browser programmatically — opening pages, finding elements, clicking buttons, and reading text — allowing automated tests to simulate genuine user interactions across Chrome, Firefox, and other browsers.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://example.com/login")
driver.find_element(By.ID, "username").send_keys("testuser")
driver.find_element(By.ID, "password").send_keys("password123")
driver.find_element(By.ID, "submit").click()
assert "Dashboard" in driver.title
driver.quit()
Common Mistakes
- Locating elements using brittle selectors (like a deeply nested CSS path) that break with minor UI changes, instead of stable IDs or data attributes.
- Not waiting for elements to load before interacting with them, causing intermittent, timing-related test failures.
- Forgetting to call driver.quit() at the end of a script, leaving browser processes running.
- Writing tests so tightly coupled to specific UI text that routine copy changes break them unnecessarily.
Professional Tip
Use explicit waits (waiting for a specific condition, like an element becoming visible) rather than fixed sleep delays. Fixed delays make tests unnecessarily slow when things load quickly, and unreliable when they load slowly.
Your Turn
Write a Selenium script (in pseudocode or real code) that navigates to a search engine, types a query into the search box, submits it, and confirms results appear.
Mini Quiz
What does Selenium WebDriver primarily allow you to do?