User Agents

Web browsers and everyday web tech, explained

Fix Safari WebDriver in Python Selenium on macOS

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. No need to specify executable_path for Safari on macOS
  4. Navigate to a website
  5. Print the title to verify it works
  6. Close the browser
  7. Common Mistakes
  8. Tips
  9. Troubleshooting
  10. Frequently Asked Questions

To fix Safari WebDriver in Python Selenium on macOS, you must enable the "Allow Remote Automation" setting in the Safari Develop menu and ensure you are using the pre-installed safaridriver binary provided by Apple. Unlike Chrome or Firefox, you do not download a separate driver executable from a website; Safari's driver is built into the operating system, but it is disabled by default for security reasons.

What You'll Need

  • A Mac running macOS with Safari installed.
  • Python installed (version 3.7 or higher recommended).
  • The Selenium library installed via pip (pip install selenium).
  • Administrator privileges to enable system-level browser settings.

Step-by-Step

  1. Enable the Develop Menu: Launch Safari. In the top menu bar, click on Safari and select Settings (or Preferences on older macOS versions). Navigate to the Advanced tab. At the bottom of the window, check the box that says Show Develop menu in menu bar.
  2. Allow Remote Automation: Now that the Develop menu is visible in the top menu bar, click on Develop. Scroll down the dropdown menu and click Allow Remote Automation. A checkmark should appear next to this option. This step is critical; without it, Selenium will throw a "SessionNotCreatedException" or a "Timed out waiting for driver" error.
  3. Verify the SafariDriver Path: Open the Terminal application. Type /usr/bin/safaridriver --version and press Enter. You should see a version number. Because the driver is located in /usr/bin/, it is already in your system PATH, meaning you do not need to specify the executable path in your Python code.
  4. Install Selenium: If you haven't already, install the Selenium package using your terminal: pip install selenium.
  5. Write the Python Script: Create a new Python file (e.g., safari_test.py) and use the following minimal code to test the connection:
    from selenium import webdriver
    

    No need to specify executable_path for Safari on macOS

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

    Close the browser

    driver.quit()
  6. Execute the Script: Run your script from the terminal using python3 safari_test.py. Safari should launch automatically, navigate to the page, and then close.

Common Mistakes

  • Downloading Third-Party Drivers: Many users search for a "SafariDriver download" similar to how they find ChromeDriver. Do not do this. Apple provides the only official driver, and it is already installed on your Mac.
  • Forgetting the Develop Menu: The most common cause of failure is skipping the "Allow Remote Automation" toggle. This setting is reset or disabled by default for security.
  • Using Outdated Selenium Versions: Using a very old version of Selenium may lead to compatibility issues with newer versions of macOS and Safari. Always ensure you are using a recent version of the library.
  • Permission Issues: Running the script in an environment where the terminal does not have permission to control the browser can lead to failures. Ensure your terminal (or IDE) has the necessary accessibility permissions in System Settings.

Tips

  • Headless Mode: Safari does not support a true "headless" mode in the same way Chrome does. If you need to run tests without a visible window, consider using a different browser or a virtual display.
  • Browser Profiles: If you need to maintain sessions or cookies, be aware that Safari WebDriver typically starts a clean session. Use the SafariOptions class to customize the launch behavior.
  • Version Matching: Ensure your macOS is up to date. Since the driver is tied to the OS, updating macOS is the only way to update the SafariDriver.

Troubleshooting

If the browser does not launch or you receive an error, try these steps:

  • Check for Hanging Processes: Sometimes a crashed session leaves a safaridriver process running in the background. Open Activity Monitor, search for "safaridriver", and force quit the process before trying again.
  • Re-toggle Automation: Uncheck "Allow Remote Automation" in the Develop menu, restart Safari, and then check it again. This can refresh the permission state.
  • Verify Python Path: If you get a ModuleNotFoundError: No module named 'selenium', ensure you are using the same Python environment where you installed the library. Try running python3 -m pip install selenium to ensure it is installed for the specific version of Python you are executing.
  • Check macOS Security Settings: Go to System Settings > Privacy & Security > Automation and ensure that your terminal or IDE (like PyCharm or VS Code) has permission to control Safari.

Frequently Asked Questions

Why is my Safari window closing immediately?

This happens if your script reaches the end of its execution or calls driver.quit(). To keep the window open, remove the quit() command or add an input prompt like input("Press Enter to close") at the end of your script.

Do I need to download a driver for Safari?

No. The SafariDriver is pre-installed on macOS. You only need to enable it via the Safari Develop menu.

Can I use Safari WebDriver on Windows or Linux?

No. Safari is only available on macOS, and the SafariDriver is a proprietary component of the macOS operating system.

What is the difference between SafariDriver and ChromeDriver?

ChromeDriver is a separate binary you download to match your Chrome version. SafariDriver is integrated into macOS and updated automatically via system updates.

Why am I getting a SessionNotCreatedException?

This is almost always caused by the "Allow Remote Automation" setting being disabled in the Safari Develop menu. Double-check that the option is checked.