Last modified: Oct 22, 2024 By Alexander Williams

WebDriver Setup (Chrome, Firefox, Safari)

Selenium WebDriver is an essential tool for automating web browsers like Chrome, Firefox, and Safari. In this guide, you will learn how to set up WebDriver for these browsers, including installation and configuration. For a broader overview of setting up Selenium, you can refer to our Installation and Setup of Selenium with Python guide.

Chrome WebDriver Setup

To automate Chrome using Selenium, you need to set up ChromeDriver. ChromeDriver acts as a bridge between Selenium and the Chrome browser.

Step 1: Download the latest version of ChromeDriver from the official ChromeDriver website.

Step 2: Extract the downloaded file and place the chromedriver executable in a directory included in your system's PATH. This will make it accessible from any location in the terminal or command prompt.

Step 3: To verify the installation, create a simple Python script:


from selenium import webdriver

# Initialize the Chrome driver
driver = webdriver.Chrome()
driver.get("https://www.google.com")
print(driver.title)
driver.quit()

Make sure to handle common issues like version compatibility. For more details, check our article on How to Fix ModuleNotFoundError: No module named 'selenium'.

Firefox WebDriver Setup

Selenium uses GeckoDriver to automate Mozilla Firefox. Below are the steps to set it up:

Step 1: Download GeckoDriver from the official GeckoDriver GitHub page.

Step 2: Extract the geckodriver executable and add it to a folder in your system's PATH. This will make it available for Selenium scripts.

Step 3: Write a Python script to test the setup:


from selenium import webdriver

# Initialize the Firefox driver
driver = webdriver.Firefox()
driver.get("https://www.google.com")
print(driver.title)
driver.quit()

This will open Firefox, navigate to Google, and print the page title. For handling elements on a webpage, you may find our guide on Python Selenium: Find Element by Link Text - Examples useful.

Safari WebDriver Setup

For Safari, the setup process is slightly different as SafariDriver comes pre-installed with macOS. You just need to enable Remote Automation in Safari’s settings.

Step 1: Open Safari, go to Preferences > Advanced, and enable "Allow Remote Automation".

Step 2: No need to download an external driver, but you might need to install the safaridriver from the terminal with the following command:


safaridriver --enable

Step 3: Test the Safari setup with a simple script:


from selenium import webdriver

# Initialize the Safari driver
driver = webdriver.Safari()
driver.get("https://www.google.com")
print(driver.title)
driver.quit()

Selenium will now automate Safari. If you encounter issues like elements not being interactable, you can refer to Python Selenium: Element Not Interactable - Handling.

Conclusion

Setting up Selenium WebDriver for Chrome, Firefox, and Safari is a crucial step for automating browser interactions. This guide covered downloading drivers, configuring them, and testing the setup with Python scripts. For further reading, visit the official Selenium documentation.