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.
pip install seleniumconda install -c conda-forge seleniumSelenium 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.
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.
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.
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.
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.
alert = driver.switch_to.alert
alert.accept()Switches to a JavaScript alert and accepts it.
driver.save_screenshot('screenshot.png')Saves a screenshot of the current browser window.
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.
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.