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 https://github.com/mozilla/geckodriver/releases.
  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')
    browser.get('https://www.google.com') 
    time.sleep(10)
    browser.quit()

2. Run Chrome Browser In Python.

  1. Download related chromedriver from https://sites.google.com/a/chromium.org/chromedriver/
  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')
    browser.get('https://www.google.com') 
    time.sleep(10)
    browser.quit()

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')    
    browser.get('https://www.google.com')
    time.sleep(10)
    browser.quit()

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.
    @classmethod
    def setUpClass(cls):
        ChromeBrowserTest.chrome_browser = webdriver.Chrome(executable_path = '/Users/zhaosong/Documents/WorkSpace/tool/chromedriver')
        print('Safari browser start.')

    # Class teardown method
    @classmethod
    def tearDownClass(cls):
        if(ChromeBrowserTest.chrome_browser!=None):
            ChromeBrowserTest.chrome_browser.quit()
            print('Safari browser quit.')
        else:
            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)>):
            self.chrome_browser.get(self.page_url)
            print("Safari browser browse page.")
            time.sleep(10)
        else:
            print('Safari browser is not started or page_url is empty.')       

if __name__ == '__main__':
    ChromeBrowserTest.page_url = 'https://www.baidu.com'
    # Create a TestSuite object.
    test_suite = unittest.TestSuite()
    # Add test function in the suite.
    test_suite.addTest(ChromeBrowserTest('browse_page'))
    # Run test suite and get test result.
    testResult = unittest.TestResult()
    test_suite.run(testResult)
    print(testResult)

Reference

  1. Python 3 Unittest Html And Xml Report Example
READ :   Selenium WebDriver - How To Perform Right Click

1 Comment


  1. thank you for ur information. this documentation is so useful for me.

    Reply

Leave a Reply

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.