How to Use Selenium with Python

Step 1: Install Selenium

Selenium can be installed easily via pip.
Run this command in your terminal:

pip install selenium

Step 2: Import and Launch a Browser

Once Selenium is installed, you can start automating your browser. Let’s begin with Chrome.

from selenium import webdriver

# Launch a new Chrome browser window
driver = webdriver.Chrome()

# Open a webpage
driver.get("https://www.google.com")

print(driver.title)

# Close the browser
driver.quit()

Step 3: Find and Interact with Elements

Selenium provides multiple ways to locate elements on a page — by ID, name, CSS selector, XPath, etc.

Here’s how to type a query into Google’s search bar and submit it:

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

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

# Locate the search box
search_box = driver.find_element(By.NAME, "q")

# Type a query and hit Enter
search_box.send_keys("Selenium Python")
search_box.send_keys(Keys.RETURN)

print(driver.title)
driver.quit()

🔎 Locator Tip: Prefer using unique IDs or CSS selectors when possible — they’re faster and less prone to breaking if the site’s layout changes.

Step 4: Wait for Elements to Load (Avoid Hard Sleeps)

Web pages often load asynchronously, so you shouldn’t use time.sleep().
Instead, use explicit waits with WebDriverWait and expected conditions:

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

driver = webdriver.Chrome()
driver.get("https://www.wikipedia.org/")

try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "searchInput"))
    )
    element.send_keys("OpenAI")
    element.submit()
finally:
    driver.quit()

This approach checks for a condition (like an element being present) repeatedly until it succeeds or times out.

Step 5: Run in Headless Mode (Optional)

Headless mode runs Chrome or Firefox without opening a visible browser window — great for servers or CI pipelines.

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

options = Options()
options.add_argument("--headless")

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

🧩 You can combine multiple arguments like --window-size=1920,1080 or --disable-gpu for better compatibility.

Step 6: Take Screenshots

Selenium can capture screenshots of your browser view — useful for debugging or test reporting.

driver.save_screenshot("page.png")

You’ll find page.png in your working directory.

Step 7: Best Practices & Tips

  • Use explicit waits instead of sleep().
  • Always quit the driver using driver.quit() to free resources.
  • Handle exceptions with try/except to make scripts more robust.
  • For large projects, use Page Object Model (POM) to structure your automation code.

Final Thoughts

Selenium is one of the most flexible browser automation tools out there. Whether you’re testing your own site, scraping dynamic content, or automating repetitive tasks, Python + Selenium offers an elegant, scriptable solution.

If you’re ready to explore more, check out:

Leave a Reply