Last Updated: November 21, 2025
Selenium WebDriver
Browser automation tool
Setup (Python)
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
# Initialize driver
driver = webdriver.Chrome()
driver.get('https://example.com')
# Find element
element = driver.find_element(By.ID, 'myId')
element.click()
# Wait for element
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, 'myId')))
# Close
driver.quit()
Locator Strategies
| Item | Description |
|---|---|
By.ID
|
Find by ID attribute |
By.NAME
|
Find by name attribute |
By.CLASS_NAME
|
Find by class name |
By.TAG_NAME
|
Find by tag name |
By.CSS_SELECTOR
|
Find by CSS selector |
By.XPATH
|
Find by XPath expression |
By.LINK_TEXT
|
Find link by exact text |
By.PARTIAL_LINK_TEXT
|
Find link by partial text |
Common Actions
# Click
driver.find_element(By.ID, 'btn').click()
# Type text
driver.find_element(By.NAME, 'username').send_keys('myuser')
# Clear input
driver.find_element(By.NAME, 'search').clear()
# Select dropdown
from selenium.webdriver.support.select import Select
select = Select(driver.find_element(By.ID, 'dropdown'))
select.select_by_visible_text('Option 1')
# Switch to iframe
driver.switch_to.frame('frameName')
driver.switch_to.default_content()
# Execute JavaScript
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
Waits
| Item | Description |
|---|---|
driver.implicitly_wait(10)
|
Wait up to 10s for elements |
WebDriverWait(driver, 10)
|
Explicit wait object |
EC.presence_of_element_located
|
Wait for element present |
EC.visibility_of_element_located
|
Wait for element visible |
EC.element_to_be_clickable
|
Wait for element clickable |
EC.title_contains
|
Wait for title |
💡 Pro Tips
Quick Reference
Always use explicit waits over implicit waits