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

AI Wrote My Code, I Skipped Testing… Guess What Happened?

AI is a fantastic tool for coding—until it isn't. It promises to save time, automate tasks, and help developers move faster. But if you trust it  too much , you might just end up doing extra work instead of less. How do I know? Because the other day, I did exactly that. The Day AI Made Me File My Own Bug I was working on a personal project, feeling pretty good about my progress, when I asked AI to generate some code. It looked solid—clean, well-structured, and exactly what I needed. So, in a moment of blind optimism, I deployed it  without testing locally first. You can probably guess what happened next. Five minutes later, I was filing my own bug report, debugging like a madman, and fixing issues on a separate branch. After some trial and error (and a few choice words), I finally did what I should have done in the first place:  tested the code locally first.  Only after confirming it actually worked did I roll out the fix. Sound familiar? If you've ever used AI-gene...

Building My Own AI Workout Chatbot: Because Who Needs a Personal Trainer Anyway?

The idea for this project started with a simple question: How can I create a personal workout AI that won't judge me for skipping leg day? I wanted something that could recommend workouts based on my mood, the time of day, the season, and even the weather in my region. This wasn't just about fitness—it was an opportunity to explore AI, practice web app engineering, and keep myself entertained while avoiding real exercise. Technologies and Tools Used To bring this chatbot to life, I used a combination of modern technologies and services (no, not magic, though it sometimes felt that way): Frontend: HTML, CSS, and JavaScript for the user interface and chatbot interaction (because making it look cool is half the battle). Backend: Python (Flask) to handle requests and AI-powered workout recommendations (it's like a fitness guru, minus the six-pack). Weather API: Integrated a real-world weather API to tailor recommendations based on live conditions (because nobody...

Smart Automation: The Art of Being Lazy (Efficiently)

They say automation saves time, but have you ever spent three days fixing a broken test that was supposed to save you five minutes? That's like buying a self-cleaning litter box and still having to scoop because the cat refuses to use it. Automation in software testing is like ordering takeout instead of cooking—you do it to save time, but if you overdo it, you'll end up with a fridge full of soggy leftovers. Many teams think the goal is to automate everything, but that's like trying to train a Roomba to babysit your kids—ambitious, but doomed to fail. Instead, let's talk about smart automation, where we focus on high-value tests that provide fast, reliable feedback, like a well-trained barista who gets your coffee order right every single time. Why Automating Everything Will Drive You (and Your Team) Insane The dream of automating everything is great until reality slaps you in the face. Here's why it's a terrible idea: Maintenance Overhead: The more ...