How to Run Selenium Python in MacOS Using Chromedriver

Selenium is a popular browser automation tool. It uses a browser client to simulate traffic to a website. It is commonly used for automating browser based web application testing and scrapping content from websites. Selenium is an open source library and it is available for use with multiple programming languages such as JavaScript, Python, Ruby, Java, Kotlin and C#. In this guide, I will show you how you can run Selenium python bindings in MacOS using Chrome browser and chromedriver.

MacOS doesn't have a native package manager. However, an open source package management tool called Homebrew is available for MacOS. We will use it to install various packages needed to get selenium up and running.

Step 1: Install Homebrew package for MacOS. See https://brew.sh/ for latest instructions on installing brew. As of writing this article, the following command will install brew,

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 2: If you don't have Chrome browser already installed, visit Chrome hompage for download and installation.

Step 3: Install python3 on your machine if it is not already available by running the following command,

brew install python

Step 4: Install selenium from MacOS command line. This assumes you already have python3 installed in your Mac machine.

sudo pip3 install selenium

Step 5: Install chromedriver using the following command. Chromedriver is a separate standalone executable that Selenium Webdriver uses to connect to and control browser actions.

brew install chromedriver --cask

By default chromedriver installed in the folder /usr/local/Caskroom/chromedriver/104.0.5112.79/chromedriver in MacOS. Please note that 104.0.5112.79 may be different as it indicates the version of the chromedriver.

Step 6: Remove quarantine for chromedriver. This is essential since by default MacOS won't allow running unsigned executables. This requires you to find the folder where chromedriver is installed and then run the following command within that folder (/usr/local/Caskroom/chromedriver/REPLACE WITH VERSION/chromedriver),

xattr -d com.apple.quarantine chromedriver

Step 7: Verify everything is setup properly. Run python3 command and then type import selenium in python3 console. If you don't see any error you are all set for writing selenium automation scripts in python!

Step 8: Write a sample python selenium script to get Google search results. Save the following program in a file named googledemo.py,

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

# connect to google.com
driver = webdriver.Chrome(options=Options())
driver.get("https://www.google.com")

# find the search input field
search_field = driver.find_element(By.TAG_NAME, "input")

# type the search string
search_field.send_keys("selenium")

# send enter key to get the search results!
search_field.send_keys(Keys.ENTER)

The above selenium python script performs a Google search for the word "selenium".

Step 9: Run the sample Google search selenium script using the following command,

python3 googledemo.py

Selenium is also useful in scenarios where simple requests package based python web scrapping is blocked due to silent JavaScript challenges such as Google reCaptcha. However some of the advanced bot protection tools are capable of detecting Selenium based automation and preventing site access.

Common Issues in Running Selenium Python on MacOS

Chrome browser and chromedriver version incompatibility.
It is important that the chromedriver version and the Chrome browser version are compatible. If they are not compatible, you will get the following message when you run a Selenium script,

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version

If you get this error, you can upgrade both Chrome browser and the corresponding chromedriver. Use the following command to upgrad chromedriver to the latest version.

brew upgrade chromedriver

Specific version of chromedriver is available here. This is useful if you want to download chromedriver version corresponding to the Google Chrome installed (if you don't want to upgrade the browser).

MacOS refusing to run chromedriver.
If you get the error "chromedriver" can't be opened because Apple cannot check it for malicious software, ensure that you have run the quarantine removal command xattr -d com.apple.quarantine chromedriver.

Recent MacOS versions such as Ventura, Monterey, Big Sur etc. includes a technology called gatekeeper which ensures only trusted software is run on the system. So if you run any software explicitly signed by Apple, you will get the above error message.