What is Selenium?

Selenium is a tool for automating web browsers. It can control browsers programmatically, making it useful for web scraping, testing, and automation.

Installation

Install Selenium:

pip install selenium

# Also need browser driver (e.g., ChromeDriver for Chrome)

Basic Usage

Control a browser:

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

driver = webdriver.Chrome()
driver.get("https://example.com")

# Find element
element = driver.find_element(By.ID, "my-id")
element.click()

driver.quit()

Finding Elements

Locate elements on page:

# By ID
driver.find_element(By.ID, "element-id")

# By class name
driver.find_element(By.CLASS_NAME, "class-name")

# By tag name
driver.find_element(By.TAG_NAME, "div")

# By CSS selector
driver.find_element(By.CSS_SELECTOR, "#id .class")

# By XPath
driver.find_element(By.XPATH, "//div[@class='content']")

Interacting with Elements

Click, type, and interact:

# Click
button = driver.find_element(By.ID, "submit")
button.click()

# Type text
input_field = driver.find_element(By.NAME, "username")
input_field.send_keys("alice")

# Get text
text = driver.find_element(By.ID, "content").text

Best Practices

  • Use explicit waits: Wait for elements to load
  • Close browser: Always call quit()
  • Handle errors: Elements may not exist
  • Use headless mode: For faster execution

Next Steps

Learn about BeautifulSoup for simpler HTML parsing, or explore Requests for HTTP requests.