September 23, 2009

Selenium RC with Python in 30 Seconds

Selenium is a suite of tools to automate web app testing across many platforms. It has various pieces (Core, RC, IDE, etc), and I struggled trying to figure out how everything fits together and works. At the end of the day, all I wanted to do was use Selenium from my Python code to drive a browser session.

Selenium ships with full tests and some good sample code, but the driver examples all contain test frameworks/runners (JUnit, unittest, etc). All I wanted was a simple way to integrate with Python. To do this, the piece I need is Selenium RC (which includes Selenium Core).

So here is the beginners' 30-second guide to getting the Python client driver working with Selenium-RC:

  1. Install Python (and add it to your path)
  2. Install Java (and add it to your path)
  3. Download Selenium RC
  4. Unzip Selenium RC and search for 'selenium-server.jar' and ''
  5. Copy them to a directory and run 'java -jar selenium-server.jar' to start the server
  6. Start writing your Python driver code! (import selenium)

So what's actually going on when you run a driver script?

Selenium-server is the core program, which also contains an integrated web server. You send HTTP requests to the server to instruct it how to drive your browser. The '' module is just a wrapper around 'httplib' that provides a Python API for interacting with the Selenium-server. You need to import the '' module from your script and then you are ready to go.

now let's test it out and see if it works. Try the following Python script. It should open Firefox and navigate to

#!/usr/bin/env python

from selenium import selenium

sel = selenium('localhost', 4444, '*firefox', '')


Unknown said...

It's good stuff, I use the IDE to run my tests since I am now working on a GUI. I then export the code out to Fitnesse and am setting up my suites to be able to run parameterized tests across our site. It's pretty cool stuff, I finally got Selenium RC and Fitnesse running as Windows services so I can access my tests from across the network.

Anonymous said...

I hear dynaTrace has a cool integration with Selenium.

Stefan Antoni said...

I have to evaluate javascript code and process it via CLI only to monitor a system. I learned that Selenium (RC) is probably the best way to do this at the moment in my specific situation. I love Python too, so your 30 seconds guide got me started right away. Thank you very much!

Lars said...

Thanks for this post... I too have struggled to understand all the parts of Selenium (particularly the Python driver) and how they fit together. This was helpful.

FYI, for those who come later: as Selenium is merged with WebDriver as of Selenium 2.0, the python part is no longer just one file, but a whole bunch. I'm currently trying to figure out how to install pip on Windows so I can install the Selenium python driver.

StanM said...

Regarding the webdriver there is a source file (release candidate 2 - rc2). That source file has all that you need.

There is a "selenium" sub folder in there. Copy + Paste that into your Python\Lib folder and after that it works as you would expect 'from selenium import selenium' / from selenium import webdriver .. etc
- hope that helps

Corey Goldberg said...

'pip install selenium'

... to get the newest bindings from PyPI.

Ashwin said...

'pip install -U selenium' for Selenium 2.13 (keep in mind Selenium 2.13 requires Python 2.6 or 2.7)

Anonymous said...

THANK YOU! I attempted to use Java with Selenium and struggled with all the .jar files and the workspace,etc. Not to mention SO many articles/tutorials with 404s, spelling errors, etc. This truly was a 30 second set-up.

Thanks for taking the time to post this!