Selenium is a powerful open-source tool for automating web browsers. Developers and testers often use it to validate web applications across different platforms and browsers. On a MacBook, installing Selenium is straightforward, especially with Homebrew, a package manager for macOS. In this guide, we’ll walk through installing Selenium, setting up a virtual environment, and installing Chromedriver, a necessary component for running Selenium with Google Chrome.
Why Install Selenium?
Selenium enables you to:
- Automate repetitive browser tasks.
- Test web applications on various browsers and platforms.
- Scrape web pages for data (if permitted).
Its flexibility and extensive language bindings make it a go-to for developers and QA engineers.
Steps to Install Selenium on a MacBook
Step 1. Verify Homebrew is install on your machine
brew --version
Verify the installation by typing:
If not install Homebrew on your machine. If already install can skip below install Homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 2. Install Python
Most MacBooks come with Python pre-installed, but it’s recommended to install the latest version using Homebrew:
brew install python
Check the Python version:
python3 --version
Step 3. Set Up a Virtual Environment
Creating a virtual environment ensures your Selenium project dependencies remain isolated. Let’s create a virtual environment called senv
:
Go to your Local Drive, Create a folder whatever you want. In my case I will create a folder name it as Selenium. Inside this folder I will set up the Virtual Environment.
python3 -m venv senv
Activate the virtual environment:
source senv/bin/activate
When the virtual environment is active, you’ll see (senv)
before your Terminal prompt.
Step 4. Install Selenium
With your virtual environment active, install Selenium via pip
:
pip install selenium
Confirm the installation:
pip show selenium
Step 4. Install Chromedriver
Selenium requires a web driver to interact with browsers. For Google Chrome, you need Chromedriver.
Deactivate the virtual environment and install the Chromedriver in Global.
deactivate //deactivate the senv that you are running
Install chromedriver
Globally using Homebrew (not within the virtual environment):
brew install chromedriver
Check if Chromedriver is installed:
chromedriver --version
Step 6. Test Your Selenium Installation
Create a Python script (test_selenium.py
) in your local project folder and add the following code:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Initialize Chromedriver
driver = webdriver.Chrome()
# Open a website
driver.get("https://www.google.com")
# Print the page title
print("Page Title is:", driver.title)
# Close the browser
driver.quit()
Run the script:
Go for Selenium Virtual Enviroment Folder >
source senv/bin/activate
Run the Selenium Test File
python3 test_selenium.py
You should see a Chrome window open and the page title printed in your Terminal.
Key Benefits of the Setup
- Virtual Environment: Keeps your Selenium dependencies organized and separate from system-wide Python packages.
- Homebrew Efficiency: Simplifies managing installations like Chromedriver.
By following this guide, you’ve successfully installed Selenium on your MacBook, created a virtual environment (senv
), and set up Chromedriver. This environment is now ready for testing, automating, or scraping tasks!
Happy coding! 🚀