Skip to main content

Introduction to Python Automation: A 10-Minute Guide to Setting Up Selenium on macOS

Are you eager to dive into web automation using Python and Selenium on macOS? Look no further! This quick guide will have you up and running in just 10 minutes.


Step 1: Check Python Installation

Before starting, ensure Python is installed on your macOS:

  1. Open Terminal.

  2. Check the Python version:

    python3 --version
    • If Python is installed, you’ll see a version number like Python 3.x.x.

    • If not, download Python or install it via Homebrew:

      brew install python

Step 2: Install Selenium

Selenium is your go-to library for web automation. Here’s how to install it:

  1. Run the following command:

    pip3 install selenium
  2. Verify the installation:

    python3 -c "import selenium; print(selenium.__version__)"

Step 3: Install a WebDriver

Selenium requires a WebDriver to interact with browsers. For this guide, we’ll use ChromeDriver:

  1. Install Google Chrome if it’s not already installed.

  2. Download ChromeDriver from the official site:

    • Select the version that matches your Chrome version.

  3. Move the ChromeDriver to a directory in your system’s PATH, e.g., /usr/local/bin/:

    mv ~/Downloads/chromedriver /usr/local/bin/
  4. Make ChromeDriver executable:

    chmod +x /usr/local/bin/chromedriver
  5. Test ChromeDriver:

    chromedriver --version

Step 4: Write Your First Selenium Script

Let’s create a Python script to automate a browser task:

  1. Open Terminal and create a file:

    nano selenium_test.py
  2. Add the following code to launch a browser and visit a website:

    from selenium import webdriver
    
    # Set up the Chrome WebDriver
    driver = webdriver.Chrome()
    
    # Open a website
    driver.get("https://www.python.org")
    
    # Print the page title
    print("Page title is:", driver.title)
    
    # Close the browser
    driver.quit()
  3. Save and exit:

    • Press Ctrl + X, then Y, and hit Enter.

  4. Run the script:

    python3 selenium_test.py

Step 5: Explore Selenium Features

Ready to take your automation to the next level? Here are some additional Selenium capabilities:

Locating Elements:

search_box = driver.find_element("name", "q")
search_box.send_keys("Selenium")
search_box.submit()

Taking Screenshots:

driver.save_screenshot("screenshot.png")

Waiting for Elements:

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

wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.NAME, "q")))

Step 6: Use Virtual Environments (Optional)

To isolate your Python projects:

  1. Create a virtual environment:

    python3 -m venv selenium_env
  2. Activate the environment:

    source selenium_env/bin/activate
  3. Install Selenium within the environment:

    pip install selenium
  4. Deactivate the environment when done:

    deactivate

Step 7: Use a Code Editor

For a better development experience, use a code editor like Visual Studio Code:

  1. Install VS Code.

  2. Add the Python extension.

  3. Open your project folder and start coding.


Summary

Congratulations! With Python and Selenium set up on macOS, you’re now equipped to automate web tasks efficiently. This guide covered:

  1. Installing Python and Selenium.

  2. Setting up ChromeDriver.

  3. Writing and running a basic Selenium script.

  4. Exploring advanced Selenium features.

Start exploring the endless possibilities of automation. Happy coding!

Comments

Popular posts from this blog

Test Case Prioritization with AI: Because Who Has Time to Test Everything?

Let's be real. Running all the tests, every time, sounds like a great idea… until you realize your test suite takes longer than the Lord of the Rings Extended Trilogy. Enter AI-based test case prioritization. It's like your test suite got a personal assistant who whispers, "Psst, you might wanna run these tests first. The rest? Meh, later." 🧠 What's the Deal? AI scans your codebase and thinks, "Okay, what just changed? What's risky? What part of the app do users abuse the most?" Then it ranks test cases like it's organizing a party guest list: VIPs (Run these first) : High-risk, recently impacted, or high-traffic areas. Maybe Later (Run if you have time) : Tests that haven't changed in years or cover rarely used features (looking at you, "Export to XML" button). Back of the Line (Run before retirement) : That one test no one knows what it does but no one dares delete. 🧰 Tools That Can Do This M...

Flaky Test Detection in AI-Based QA: When Machine Learning Gets a Nose for Drama

You know that one test in your suite? The one that passes on Mondays but fails every third Thursday if Mercury's in retrograde? Yeah, that's a flaky test. Flaky tests are the drama queens of QA. They show up, cause a scene, and leave you wondering if the bug was real or just performance art. Enter: AI-based QA with flaky test detection powered by machine learning. AKA: the cool, data-driven therapist who helps your tests get their act together. 🥐 What Are Flaky Tests? In technical terms: flaky tests are those that produce inconsistent results without any changes in the codebase. In human terms: they're the "it's not you, it's me" of your test suite. 🕵️‍♂️ How AI & ML Sniff Out the Flakes Machine Learning models can be trained to: Track patterns in test pass/fail history. Correlate failures with external signals (e.g., network delays, timing issues, thread contention). Cluster similar failures to spot root causes. La...

AI Visual Regression Testing: Because Your UI Shouldn’t Ghost You Overnight

Imagine spending weeks perfecting your app's UI.  The buttons are sleek, the layout's clean, and everything looks like it could win a design award. You go to bed feeling like a coding Picasso. Then… you wake up. Your buttons are misaligned. Your logo is somewhere in Ohio. And that "Sign Up" button? It's decided to explore a life of solitude. Welcome to the horror movie called Visual Regression,  where your UI goes rogue and doesn't text back. Enter AI: Your Pixel-Picking Sidekick Visual regression testing with AI compares snapshots of your app's UI over time, automatically detecting unintended visual changes like: A rogue font size tweak Padding that got a little too cozy Missing elements that got Thanos-snapped But instead of you manually comparing screenshots like a paranoid ex stalking your design system, AI handles it with laser focus and zero drama. How It Works (Without Making You Cry) Take a baseline scree...