Selenium

Language: Python

Web Automation

Selenium was originally developed in 2004 by Jason Huggins at ThoughtWorks as an internal tool for automated testing of web applications. Over time, it evolved into a widely-used framework for browser automation, supporting multiple programming languages and browser drivers. Selenium WebDriver became the standard API for reliable cross-browser automation.

Selenium is a powerful Python library for automating web browsers. It allows you to programmatically interact with web pages, simulate user actions, and perform browser testing across multiple browsers.

Installation

pip: pip install selenium
conda: conda install -c conda-forge selenium

Usage

Selenium allows you to control browsers using WebDriver. You can navigate pages, click buttons, fill forms, extract text, take screenshots, handle alerts, and perform automated testing. It supports Chrome, Firefox, Edge, Safari, and other major browsers.

Opening a webpage

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://example.com')
print(driver.title)
driver.quit()

Launches Chrome, navigates to the URL, prints the page title, and closes the browser.

Finding elements by ID or Name

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

driver = webdriver.Chrome()
driver.get('https://example.com')
element = driver.find_element(By.ID, 'username')
element.send_keys('myuser')
driver.quit()

Locates an input field using its ID and types text into it.

Clicking a button

from selenium.webdriver.common.by import By
button = driver.find_element(By.XPATH, '//button[text()="Submit"]')
button.click()

Finds a button by XPath and simulates a click.

Waiting for elements

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, 'result')))

Waits up to 10 seconds for an element to appear before proceeding.

Handling alerts

alert = driver.switch_to.alert
alert.accept()

Switches to a JavaScript alert and accepts it.

Taking screenshots

driver.save_screenshot('screenshot.png')

Saves a screenshot of the current browser window.

Headless mode

from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)

Runs Chrome in headless mode for faster automation without opening a visible window.

Error Handling

NoSuchElementException: Verify the selector used to locate the element and ensure it exists on the page.
WebDriverException: Message: 'chromedriver' executable needs to be in PATH: Download the correct WebDriver for your browser and ensure it is in your system PATH.
TimeoutException: Increase the wait time or check that the expected condition is correct.

Best Practices

Use explicit waits (`WebDriverWait`) instead of arbitrary sleep times for stability.

Close or quit browser instances to free resources.

Use headless mode for automation scripts that don't require UI interaction.

Leverage browser-specific options and capabilities for optimized performance.

Keep WebDriver versions compatible with the browser version installed.