Getting Started with WebDriver in Python on OSx

Posted by Jim Holmes

Python is a widely used programming language that’s been around since 1991. Python runs on many, many platforms. It’s interpreted versus compiled, and has an emphasis on readability. Where Java, C#, and many other languages use braces( “{“ and “}” ) to denote code blocks, Python uses whitespace. CPython, the reference implementation of the language, is open source and managed by the Python Software Foundation.

This article is one in a series showing how to get WebDriver working in various editors and language platforms.

For an overview of how WebDriver works, please see the section “WebDriver Overview” in the blogpost “Getting Started with Webdriver/Selenium for Java in Eclipse” here.

The Components You'll Need

To create and run WebDriver tests in Python you’ll need the following components:

  • Python (This article uses 3.6.0a3 for examples)
  • An editor
  • WebDriver for Python
  • Mozilla’s Gekodriver proxy for Firefox

Choose Your Editor

Like Java, C#, Ruby, and most all other languages, Python’s code files are simply text files. You can create and edit the .py files in any plain text editor you like. There are a tremendous number of great editors for OSx/*nix, including Vim, Emacs, TextMate, Sublime, and many others.

We’ll stay away from the Editor Wars and just suggest you try several to find one that matches your style and needs!

Get Python

OSx Sierra comes with Python 2.7 already installed. You absolutely can use that to write WebDriver; however, installing a newer version of Python is simple.

If you don’t already have it, install pyenv, a Python environment manager. From a Terminal prompt use OSx’s homebrew to install it:

brew install pyenv

Pyenv install —list will show you which versions are available to install. Install Python with

Pyenv install 3.6.0a3

Adding WebDriver

Next you’ll need WebDriver. Python’s package manager, pip, makes it easy. From the same terminal use

pip install webdriver

Adding The Firefox Driver Proxy

As noted in the WebDriver Overview in the first post of this series, you’ll need to have a proxy for your test to talk to the actual browser. This example uses Firefox, so you’ll need to grab the appropriate proxy. Proxies for all WebDriver-supported browsers are listed on the SeleniumHQ’s list of Third Party Drivers. Firefox’s driver is part of Mozilla’s Gecko Driver releases. Make sure to grab the driver that’s appropriate for the version of Windows you’re running (x32, x64).

Download the zip file and extract the driver to a location on your system. You’ll need to add that location to your system’s PATH environment variable.

Writing Your First Test

Creating your first test in Python is as simple as opening a new text file. The mechanics of that are specific to the editor you’re using. In Vim you would use something akin to

:e ~/Documents/Workspace/Python/webdriver_basics.py

This would open a new buffer for your test file in the editor.

A Simple Test

Below is a complete test case that starts a browser locally, executes a very simple test, then closes out the browser instance. The example is extremely simple and doesn’t follow normal practices like using Page Object Patterns. This is example code, not production code!

import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By

class WebDriverPythonBasics(unittest.TestCase):
def setUp(self):
    self.browser = webdriver.Firefox()

def test_saucelabs_homepage_header_displayed(self):
    self.browser.get('http://saucelabs.com')
    header = self.browser.find_element(By.ID, 'site-header')
    self.assertTrue(header.is_displayed())


def tearDown(self):
    self.browser.close()

if name == 'main':
unittest.main()

Running The Test

Running the test is a matter of simply typing

python webdriver_basics.py

from the directory where you created the file.

You’ll see Firefox start, navigate to the Sauce Labs home page, and close. Note that you will not see a message that the test has passed - the extremely simple example doesn’t provide feedback unless something fails. There are a plethora of different approaches for running larger test suites that give more precise results for production use.

Running Python WebDriver Test

Wrapping It All Up

In this post you learned a bit about installing Python on your OSx system, and writing a very simplistic WebDriver test.

Good luck with your explorations of WebDriver!


Free Trial

Get access to a free 14-day trial version, or contact Sales for more information.