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...

NLP Test Generation: "Write Tests Like You Text Your Mom"

Picture this: You're sipping coffee, dreading writing test cases. Suddenly, your QA buddy says, "You know you can just tell the AI what to do now, right?" You're like, "Wait… I can literally write: 👉 Click the login button 👉 Enter email and password 👉 Expect to see dashboard " And the AI's like, "Say less. I got you." 💥 BOOM. Test script = done. Welcome to the magical world of Natural Language Processing (NLP) Test Generation , where you talk like a human and your tests are coded like a pro. 🤖 What is NLP Test Generation? NLP Test Generation lets you describe tests in plain English (or whatever language you think in before caffeine), and the AI converts them into executable test scripts. So instead of writing: await page. click ( '#login-button' ); You write: Click the login button. And the AI translates it like your polyglot coworker who speaks JavaScript, Python, and sarcasm. 🛠️ Tools That ...

Self-Healing Locators: Your Automated QA MVP with a Sixth Sense

Let's face it: UI changes are like that one coworker who swears they'll stick to the plan… then shows up Monday morning with bangs, a new wardrobe, and a totally different personality. If you've ever maintained UI automation tests, you know the pain: One tiny change — a renamed id , a tweaked class name, or heaven forbid, a redesigned page — and BAM! Half your tests are failing, not because the feature is broken… but because your locators couldn't recognize it with its new haircut. Enter: Self-Healing Locators 🧠✨ 🧬 What Are Self-Healing Locators? Think of self-healing locators like the Sherlock Holmes of your test suite. When a locator goes missing in action, these clever AI-powered systems don't throw a tantrum — they investigate . Instead of giving up, they: Notice something's changed, Analyze the page, Find similar elements using AI and ML magic , And update the locator on the fly , so your test passes like nothing ever hap...