Andromeda
Note

Selenium Browser Automation (Python)

Definition

A powerful library that allows Python to launch and control a live web browser, enabling interaction with JavaScript-heavy sites and automated form filling.

Why It Matters

Selenium is the ‘robot arm’ for the modern web; it allows for the automated testing and data-scraping of JavaScript-heavy sites that are ‘invisible’ to simpler tools, making it indispensable for modern web development and research.

Core Concepts

  • WebDriver: Launches a specific browser (e.g., webdriver.Firefox() or webdriver.Chrome()).
  • Locating Elements: Similar to BeautifulSoup but acts on the live page: browser.find_element(By.CSS_SELECTOR, '...').
  • Interactions:
    • .click(): Simulates a mouse click.
    • .send_keys('text'): Types text into a field.
    • Keys module: Allows sending special keys like Keys.ENTER, Keys.TAB, Keys.DOWN.
  • Navigation: .back(), .forward(), .refresh(), .quit().
  • Example Usage:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

# Initialize the browser
browser = webdriver.Chrome()
browser.get('https://www.google.com')

# Find a search box, type a query, and press Enter
search_box = browser.find_element(By.NAME, 'q')
search_box.send_keys('Python automation')
search_box.send_keys(Keys.ENTER)

# Close the browser
browser.quit()

Connected Concepts