Getting Started with WebDriver in Python on OSx

This article will show you how to get WebDriver working in Python.

Python is popular as a quick-to-write, quick-to run scripting languages. It does not separate the compile step from the run step, and is relatively easy to learn. As a result, Python is popular for “small” computer programs, scripts, and prototyping, making it ideal for writing tests that drive a more complex tool. This focuses on how to get selenium up and running in Python in OSX.One major difference in programming languages is that where Java, C#, others use braces( “{“ and “}” ) to denote code blocks, Python uses whitespace.

For an overview of how WebDriver works, please see the section “WebDriver Overview” in the blogpostGetting 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.7.21 for examples)
  • An editor
  • WebDriver for Python
  • The ChromeDriver executable

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 python

Now you have three commands - python, python2, and python3. Learn the version of python with this code:

Python -V

As of this publication the version is 3.7.2.

Adding WebDriver

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

pip3 install selenium
If you get an error, you may need to perform the pip install
for python3. Try this:
curl -O https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py

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 Chrome 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.  Download Chromedriver, unzip it and put it in your OSX Path. You can tell this is successful by typing “Chromedriver <ENTER>” intot the command prompt. You will see a message like “Starting ChromeDriver …”

Download the zip file and extract the driver to a location on your system. You may 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 sacrifices simplicity for “goodness”, and does not follow normal practices like the Page Object Pattern. As a result it is easy to read in one web page.. If you’d like, you can download the code from github

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


class WebDriverPythonBasics(unittest.TestCase):

   def setUp(self):        self.browser = webdriver.Chrome()

   def test_saucelabs_homepage_header_displayed(self):        self.browser.get("https://www.saucelabs.com")        element = self.browser.find_element(By.XPATH, '//a[text()="Platforms"]');        self.assertTrue(element.is_displayed());        element.click();        pricing_link = self.browser.find_element(By.XPATH, '//a[text()="Pricing"]');        self.assertTrue(pricing_link.is_displayed());        pricing_link.click();

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

if __name__ == '__main__':        unittest.main()

Running The Test

Running the test is a matter of simply typing

python3 simpletest.py

from the directory where you created the file.

You’ll see Chome start, navigate to the Sauce Labs home page, click the “Platforms” link, click the “Pricing” link, and close. The tests verify that the links exist and are visible.

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!

Written by

Matthew Heusser

Topics

Programming languagesSeleniumGithubFrameworksGet Started/Guide