Automating Web Tasks with Selenium: A Hands-On Guide

If you’ve ever wasted hours clicking through websites or verifying page titles manually, Selenium is about to become your best friend. This powerful tool lets you automate browser actions—like logging in, scraping data, or testing web apps—with just a few lines of Python. Here’s how to get it working.

How Selenium Fits into Web Automation

Think of Selenium as a robot that controls your browser. It can:

  • Open pages
  • Fill forms
  • Click buttons
  • Extract data
  • Test if a website behaves as expected

Unlike manual testing, Selenium runs tasks in seconds—perfect for repetitive checks or large-scale scraping.

How APIs and Browsers Work Together

When you type a URL (like google.com), here’s what happens behind the scenes:

  1. Your browser (GUI) sends a request.
  2. The API (middleman) fetches data from Google’s servers.
  3. The database (backend) delivers the page content.

Selenium automates the GUI layer—interacting with what you see on-screen. For deeper testing (like checking API responses or database entries), you’d pair it with tools like requests or SQLAlchemy.

Setting Up Selenium

1. Install the Package

Run this in your terminal (PyCharm’s works too):

bash

Copy

Download

pip install selenium

2. Download Browser Drivers

Selenium needs a driver to control your browser. Grab one for:

Pro Tip: Save the .exe file in a dedicated folder (e.g., C:/drivers). Avoid desktop—paths with spaces can cause errors.

Your First Automation Script

Goal: Open Google, check if the page title is correct, then close the browser.

The Code

python

Copy

Download

from selenium import webdriver

# 1. Launch Edge (update the path to your driver)

driver = webdriver.Edge(executable_path=”C:/drivers/msedgedriver.exe”)

# 2. Navigate to Google

driver.get(“https://www.google.com”)

# 3. Check the title

actual_title = driver.title

expected_title = “Google”

if actual_title == expected_title:

    print(“✅ Title matches! Test passed.”)

else:

    print(f”❌ Got ‘{actual_title}’ instead of ‘Google’. Test failed.”)

# 4. Shut it down

driver.quit()

What’s Happening?

  • webdriver.Edge() starts a new Edge window.
  • .get() loads the URL.
  • .title extracts the page title (found in the HTML <title> tag).
  • driver.quit() closes the browser cleanly.

Troubleshooting:

  • If the script crashes, check:
    • Driver path is correct.
    • Browser version matches the driver (e.g., Edge 120 needs msedgedriver 120).

Real-World Example: Automating a Login

Let’s test a demo login page (like OrangeHRM):

python

Copy

Download

from selenium import webdriver

from selenium.webdriver.common.by import By

driver = webdriver.Chrome(“C:/drivers/chromedriver.exe”)

driver.get(“https://opensource-demo.orangehrmlive.com/”)

# Fill credentials

driver.find_element(By.NAME, “username”).send_keys(“Admin”)

driver.find_element(By.NAME, “password”).send_keys(“admin123”)

# Click login

driver.find_element(By.TAG_NAME, “button”).click()

# Verify login

if “dashboard” in driver.current_url:

    print(“✅ Login successful!”)

else:

    print(“❌ Login failed.”)

driver.quit()

Key Improvements:

  • By.NAME locates fields by HTML attributes (more reliable than XPaths).
  • .current_url confirms post-login redirects.

When to Use Selenium vs. Alternatives

  • Selenium: Best for dynamic sites (JavaScript-heavy pages like Gmail).
  • Requests + BeautifulSoup: Faster for static page scraping (Wikipedia, blogs).
  • PyAutoGUI: Use only if no other option (e.g., legacy Java apps).

Final Advice

  1. Start small—automate one action (like a search) before complex flows.
  2. Add delays with time.sleep(2) if pages load slowly.
  3. Use headless mode for speed (options.add_argument(“–headless”)).

Selenium turns tedious web tasks into one-click operations. Now go automate something boring—you’ve earned the time back.

Leave a Comment