Skip to content

Latest commit

 

History

History
343 lines (243 loc) · 6.77 KB

File metadata and controls

343 lines (243 loc) · 6.77 KB

Selenium WebDriver Setup Guide

Installing WebDriver

Chrome/Chromium

Step 1: Check Chrome Version

macOS:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version

Linux:

google-chrome --version

Windows:

"C:\Program Files\Google\Chrome\Application\chrome.exe" --version

Step 2: Download ChromeDriver

  1. Visit ChromeDriver Downloads
  2. Download version matching your Chrome browser version
  3. Extract the chromedriver executable

Step 3: Setup ChromeDriver

# macOS/Linux: Copy to drivers directory
cp ~/Downloads/chromedriver ./drivers/
chmod +x ./drivers/chromedriver

# Windows: Copy chromedriver.exe to drivers directory
copy C:\Downloads\chromedriver.exe .\drivers\

Step 4: Verify Installation

# macOS/Linux
./drivers/chromedriver --version

# Windows
.\drivers\chromedriver.exe --version

Firefox/GeckoDriver

Step 1: Download GeckoDriver

  1. Visit GeckoDriver Releases
  2. Download latest version for your OS
  3. Extract the geckodriver executable

Step 2: Setup GeckoDriver

# macOS/Linux
cp ~/Downloads/geckodriver ./drivers/
chmod +x ./drivers/geckodriver

# Windows
copy C:\Downloads\geckodriver.exe .\drivers\

Step 3: Verify Installation

# macOS/Linux
./drivers/geckodriver --version

# Windows
.\drivers\geckodriver.exe --version

Automatic WebDriver Management

Using webdriver-manager

Automatically download and manage WebDrivers:

pip install webdriver-manager

Usage:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service

# Automatic driver management
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

Benefits:

  • Automatic driver version matching
  • No manual downloads
  • Cross-platform compatible

Configuration

Chrome Options

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()

# Headless mode (no GUI)
options.add_argument("--headless")

# Incognito/Private mode
options.add_argument("--incognito")

# Disable extensions
options.add_argument("--disable-extensions")

# Disable notifications
options.add_argument("--disable-notifications")

# Disable images (faster loading)
options.add_argument("--blink-settings=imagesEnabled=false")

# Custom user agent
options.add_argument(
    "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
)

# Custom window size
options.add_argument("--window-size=1920,1080")

# Disable GPU (for CI/CD)
options.add_argument("--disable-gpu")

# Create driver with options
driver = webdriver.Chrome(
    executable_path="./drivers/chromedriver",
    options=options
)

Firefox Options

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()

# Headless mode
options.add_argument("--headless")

# Private mode
options.add_argument("-private")

# Custom user agent
options.set_preference(
    "general.useragent.override",
    "Mozilla/5.0 (Custom User Agent)"
)

# Create driver
driver = webdriver.Firefox(
    executable_path="./drivers/geckodriver",
    options=options
)

Implicit vs Explicit Waits

Implicit Wait (Not Recommended)

driver.implicitly_wait(10)  # Wait up to 10 seconds
element = driver.find_element(By.ID, "element_id")

Explicit Wait (Recommended)

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

wait = WebDriverWait(driver, 10)
element = wait.until(
    EC.presence_of_element_located((By.ID, "element_id"))
)

Common Wait Conditions

# Element present in DOM
EC.presence_of_element_located((By.ID, "id"))

# Element visible on page
EC.visibility_of_element_located((By.ID, "id"))

# Element clickable
EC.element_to_be_clickable((By.ID, "id"))

# Element has text
EC.text_to_be_present_in_element((By.ID, "id"), "text")

# URL contains text
EC.url_contains("text")

# Title contains text
EC.title_contains("text")

Locator Strategies

from selenium.webdriver.common.by import By

# ID
driver.find_element(By.ID, "element_id")

# Name
driver.find_element(By.NAME, "element_name")

# Class Name
driver.find_element(By.CLASS_NAME, "class_name")

# CSS Selector
driver.find_element(By.CSS_SELECTOR, ".class > p")

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

# Link Text
driver.find_element(By.LINK_TEXT, "Click Here")

# Partial Link Text
driver.find_element(By.PARTIAL_LINK_TEXT, "Click")

# Tag Name
driver.find_element(By.TAG_NAME, "button")

Browser Profile Management

Chrome User Profile

options = Options()
options.add_argument(
    "user-data-dir=/path/to/chrome/profile"
)
driver = webdriver.Chrome(options=options)

Firefox Profile

from selenium.webdriver.firefox.service import Service

profile = webdriver.FirefoxProfile()
driver = webdriver.Firefox(
    firefox_profile=profile,
    executable_path="./drivers/geckodriver"
)

Environment Variables

# Set WebDriver path in environment
export CHROMEDRIVER_PATH="./drivers/chromedriver"
export GECKODRIVER_PATH="./drivers/geckodriver"
import os

chrome_path = os.environ.get('CHROMEDRIVER_PATH', './drivers/chromedriver')
driver = webdriver.Chrome(executable_path=chrome_path)

Troubleshooting

"ChromeDriver version mismatch" Error

# Check Chrome version
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version

# Download matching ChromeDriver
# Visit: https://chromedriver.chromium.org/downloads

"WebDriver Error: Session not created"

# Solution: Update WebDriver or Chrome
# Or disable certain features
options = Options()
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(options=options)

"Element not found" Error

# Use explicit waits instead of find_element
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "element_id"))
)

Performance Tips

options = Options()

# Run headless (faster)
options.add_argument("--headless")

# Disable images (faster loading)
options.add_argument("--blink-settings=imagesEnabled=false")

# Disable extensions
options.add_argument("--disable-extensions")

# Single process (faster but less stable)
options.add_argument("--single-process")

driver = webdriver.Chrome(options=options)