safari preferences advanced dialog

Python – How To Launch Safari, Firefox, Chrome In Selenium Webdriver

When you write automation test case with python, you always need to test the functions in different web browsers such as Firefox, Chrome, Safari and IE. This example will tell you how to configure and start related web browser in python source code.

1. Run Firefox Browser In Python.

  1. Download related geckodriver from
  2. Unzip the download file to a local directory.
  3. Use below python code to start a Firefox web browser. If you meet any error, you can read article How To Resolve Webdriverexception Geckodriver Executable Needs To Be In Path
    from selenium import webdriver
    import time
    browser = webdriver.Firefox(executable_path = '/Users/zhaosong/Documents/WorkSpace/tool/geckodriver')

2. Run Chrome Browser In Python.

  1. Download related chromedriver from
  2. Unzip chromedriver executable file to a local folder.
  3. Run below code to start google chrome web browser in python.
    import time
    from selenium import webdriver
    browser = webdriver.Chrome(executable_path = '/Users/zhaosong/Documents/WorkSpace/tool/chromedriver')

3. Run Safari Browser In Python.

Safari driver is included with MacOS Sierra by default, it is saved in folder /usr/bin. You can run which safaridriver in command line to get it’s saved folder path. So you do not need to download and install safari driver.

But before you can start Safari web browser in python code, you need do following settings, otherwise you may encounter below error message when run python code.

selenium.common.exceptions.SessionNotCreatedException: Message: Could not create a session: You must enable the 'Allow Remote Automation' option in Safari's Develop menu to control Safari via WebDriver.

3.1 Enable Allow Remote Automation Option In Safari Web Browser.

  1. Open a safari web browser, then click Safari —> Preferences… menu item in the top menu bar.
    macos safari preferences menu item
  2. Click Advanced icon in the popup dialog. Check Show Develop menu in menu bar checkbox. After this you can see a
    safari preferences advanced dialog
  3. Then there will display a Develop menu item in the Safari web browser menu bar. Click Develop —> Allow Remote Automation menu item to select it.
    safari develop allow remote automation menu item
  4. Now run below python code to start the Safari web browser.
    from selenium import webdriver
    import time
    browser = webdriver.Safari(executable_path = '/usr/bin/safaridriver')    

4. Run Selenium WebDriver In Python Unittest.

  1. Import python build-in unittest module.
  2. Create a python class which extends unittest.TestCase.
  3. Start the web browser driver in TestCase class setUpClass method.
  4. Use the web browser driver in TestCase class test functions.
  5. Close and quit the web browser driver in TestCase class tearDownClass method.
Created on Aug 31, 2018
@author: zhaosong

from selenium import webdriver
import time
import unittest

class ChromeBrowserTest(unittest.TestCase):

    chrome_browser = None
    page_url = ''

    # Class setup method.
    def setUpClass(cls):
        ChromeBrowserTest.chrome_browser = webdriver.Chrome(executable_path = '/Users/zhaosong/Documents/WorkSpace/tool/chromedriver')
        print('Safari browser start.')

    # Class teardown method
    def tearDownClass(cls):
            print('Safari browser quit.')
            print('Safari browser is not started.')   
    # This is the test function.    
    def browse_page(self):
        if(self.chrome_browser!=None and len(self.page_url)>0):
            print("Safari browser browse page.")
            print('Safari browser is not started or page_url is empty.')       

if __name__ == '__main__':
    ChromeBrowserTest.page_url = ''
    # Create a TestSuite object.
    test_suite = unittest.TestSuite()
    # Add test function in the suite.
    # Run test suite and get test result.
    testResult = unittest.TestResult()


  1. Python 3 Unittest Html And Xml Report Example

1 thought on “Python – How To Launch Safari, Firefox, Chrome In Selenium Webdriver”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.