Skip to content

Automated web testing with Selenium’s python webdriver

December 11, 2013

I recently started working as an automation engineer in Kampyle, a feedback service and BI startup. My role here is to establish a system for automated testing of the company’s various products.

I’ve done my fair share of automation before, though not in this field. So I’m still learning, but after working here for around a week, I already have several automated tests that seem to work nicely. During this week of research, learning and writing, I found that there’s some nice sources out there for using the tools my automated system is based on: Selenium and it’s python API.

But even there is some nice documentation and lots of useful stack overflow posts, I thought it would be nice to write a short intro of how to write selenium based tests for web services in Python. So here goes:

Install Selenium and Selenium-Python:

Selenium is a wonderful tool for emulating a user’s interaction with web browsers. You can either record and replay your actions as a real user with Selenium IDE, use one of the supported programming languages and write scripted tests using the WebDriver, or even establish a server hub for multi-platform testing.

To write test scripts in Python, you need to install the python-selenium module. This is easy through the pip package manager. If you don’t have pip, install it on your system. In Debian based Linux systems:

sudo apt-get install python-pip

Then use it to install the selenium module:

pip install selenium

You might need more dependencies, depending (ha!) on the type of browser you’re using. Read about this on the API’s page.

Getting into grips with Selenium’s python API

The APIs enable you to interact with browser’s through Selenium. Selenium takes care of sending your standard python commands to each browser type, despite the variations in each browser’s architecture. So with a nice relatively generic script, you can run scripts for Firefox, Chrome, IE, Safari etc, in different operating systems.

Selenium does that using a “WebDriver”, its standard representation for all browsers.This webdriver can perform various actions such as loading pages, searching for elements etc. These element objects can then send data depending on their type (textboxes, checkboxes, etc) and elicit browser events such as clicks.

Here’s a short example showing how to create a firefox type webdriver, then get a page:

from selenium import webdriver

# Set Selenium firefox browser object
browser = webdriver.Firefox()

# Step 1: Navigate to page
browser.get( page_address )

If you run this, you’ll see a new firefox window pop up and navigate to the page you specified. Simple and easy.
And if you want to test that the page loaded successfully, you can use unit tests and assert commands:

    msg = "Got to page"

A whole bunch of interesting tests

Test that you found a specific element in the page, through it’s id:

import unittest
from   selenium import webdriver
from   selenium.common.exceptions import NoSuchElementException

# Set Selenium firefox browser object
browser = webdriver.Firefox()

# Navigate to desired page
browser.get( desired_page )

element = browser.find_element_by_id( element_id )
except NoSuchElementException: "found: %s" % element_id )

Check if a specific element is of a desired class:

# Assert that the element is of a specific class
"desired class",
msg = "F.3. element is of desired class"

Now for a slightly more complicated one: logging in by typing a username and password, pressing the submit button, then making sure login was successful:

import unittest
from   selenium import webdriver
from   selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get( login_page )

# Fetch username, password input boxes and submit button
# This time I'm now testing if the elements were found.
# See the previous exmaples to see how to do that.
username = browser.find_element_by_id( "username" )
password = browser.find_element_by_id( "password" )
submit   = browser.find_element_by_id( "submit"   )

# Input text in username and password inputboxes
username.send_keys( "me"         )
password.send_keys( "mykewlpass" )

# Click on the submit button

# Create wait obj with a 5 sec timeout, and default 0.5 poll frequency
wait = WebDriverWait( browser, 5 )

# Test that login was successful by checking if the URL in the browser changed
page_loaded = wait.until_not(
lambda browser: browser.current_url == login_page
except TimeoutException: "Loading timeout expired" )

# Assert that the URL is now the correct post-login page
msg = "Successful Login"

What’s next?

These few commands and tests provide quite a lot of options by themselves, but there’s plenty more to explore in the Selenium python module API (that for some reason is rather hard to find directly form google!).

There’s also the possibility of using the Selenium Server (formerly AKA “Selenium RC” or “remote control”) to create a hub for running distributed tests on multiple platforms. I spent a frustrating couple of days trying to get a basic server-client system to work, and ended up giving up and using a simple webdriver just to get the hang of the system.

I’ll probably end up writing my own hub server later on, but in the mean time, there’s a whole lot of automated tests to write!

  1. sarayu permalink

    I have a question about installation. I working on windows 7.My machine has Python 3.3 verson software and i do have experience using selenium and java in Eclipse..In My recent project comming up with Python. I installed and started using Python 3.3..I am not able to figurie it out , how to add selenium jar file to python…i have experiece working on python..I googled i am not able to find proper information


    • Hi Sarayu,

      If you’re using python you don’t need the Selenium jar file at all.
      If you’re working in a linux environment, install pip:
      sudo apt-get install python-pip

      Then install the Selenium python module using pip:
      sudo pip install selenium

      Then you’ll be able to import this module in your python scripts.

      For further details, see the official documentation:


  2. Logigen permalink

    i have a problem on line 27 : NameError: global name ‘login_page’ is not defined.

    • Of course, you should assign to this variable whichever actual string URL you need to test.

  3. Anirudh permalink


    Is there any advantage of using Selenium with Python over using with Java? Given the widespread community for Java?

    • Hi Anir,

      I can’t really say, since I’m not a Java programmer and hardly a Selenium expert.

      Since both APIs give you access to practically the same functinality, I guess this is mostly an issue of personal preference and the adaptation to the general software environment one has.

      One advantage of the Java implementation is that Selenium itself is, to my knowledge, written in Java, something that might make things easier to expand on, and might make the API a bit better, but this is mostly guesswork.

  4. Anirudh permalink

    Sure, Man! Thanks a lot for responding. I like your blog.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: