Wednesday, January 11, 2006

Running Selenium in Python Driven Mode

Update August 2007

For people who are stumbling on this blog post via searches: this post is outdated. There is no Python Driven Mode' in Selenium anymore. Instead, you should use Selenium RC, which offers the same capabilities and much more.

Original post:

I blogged about this before, but here it is again, in a shorter and hopefully easier to understand format. This post was prompted by questions on the selenium-users at mailing list on how to run Selenium in Driven Mode using Python. By Driven Mode, people generally understand scripting Selenium commands as opposed to writing them in HTML tables. Scripting offers obvious advantages, such as real programming language constructs and idioms.

Here is a step-by-step HOWTO on running Selenium in Driven Mode using Python. I just tried on a Windows XP computer (see this blog post of mine for details on running it on *nix):

1. Download and install Twisted version 1.3 (this is important, because things have been known not to work so well with Twisted 2.x). Old versions of Twisted, including 1.3, can be downloaded from here. I used Twisted 1.3 for Python 2.4.

2. Check out the Selenium source code:

svn co selenium

3. Let's assume you checked out the code in C:\proj\selenium. You need to copy the contents of C:\proj\selenium\code\javascript (the contents, not that directory itself) into the C:\proj\selenium\code\python\twisted\src\selenium\selenium_driver directory.

4. Open a DOS command prompt and cd into C:\proj\selenium\code\python\twisted\src\selenium, then run:


You should see something like this printed at the console (if you don't have PyCrypto already installed):

C:\Python24\lib\site-packages\twisted\protocols\ RuntimeWarning: PyCrypto not available - proceeding with non-cryptographically secure random source 1
Starting up Selenium Server...

To verify that the Twisted-based Selenium server is up and running, open this URL in a browser: http://localhost:8080/selenium-driver/SeleneseRunner.html

You should see the Selenium Functional Test Runner page at this point.

5. Open another DOS command prompt and cd into C:\proj\selenium\code\python\twisted\src\selenium, then run:


At this point, a browser window will be automatically opened, the browser will connect to the SeleneseRunner.html page and will execute the test commands specified in the script. You should see the bottom frame of the SeleneseRunner.html page open the Google home page, then enter a search phrase and run the search. Here is what it looks like in my case (click on the image to see a larger image):

Also, the google-test-xmlrpc script printed this on the console:

Actual value 'Selenium ThoughtWorks - Google Search' did not match 'Google Search: Selenium ThoughtWorks'
test complete

So you can see that the one of the test commands -- app.verifyTitle('Google Search: Selenium ThoughtWorks') -- actually fails, because Google changed the way they construct the title for search result pages.

Note that you can drive Selenium from any language that offers XML-RPC client libraries. If you look in the twisted\src\examples directory, you'll notice that besides the Python client script, a Ruby script and a Perl script are also included. You can just as easily write Java code to do the same thing.

That's about it. Let me know if you have problems following these steps. Leave a comment on this blog or write to the selenium-users at mailing list.


Ilya said...

Hi. I use Selenium rev. 679 and Twisted 2.1 for Python 2.4 on Ubuntu 5.10. I've stuck with this error:

exceptions.AttributeError: class SeleniumInterpreter has no attribute '__adapt__'

After googling I read that problem appears on with Twisted 2.x. But Ubuntu and other recent distros doesn't have the 1.3 version of Twisted.

This bug can be fixed or workarounded without installing Twisted 1.3 from scratch?

Anonymous said...

Twisted download link should probably point to instead of

Barton said...

I am getting the following error:
Traceback (most recent call last):
File "", line 23, in ? from twisted.internet import win32eventreactor File "C:\Python24\Lib\site-packages\twisted\internet\", line 58, in ?
from win32file import WSAEventSelect, FD_READ, FD_WRITE, FD_CLOSE, \
ImportError: No module named win32file

I have reinstalled twisted and checked out the source code again to no avail.
Any help will be appreciated.

Grig Gheorghiu said...


You need the Python win32 extensions package from

Or you can install the ActiveState Python distro, which includes the win32 extensions out of the box.


Barton said...

Thank you! When I run the script, I get ERROR: Result queue was empty on the console. It repeats 7 times and then says Test Complete. I cannot run it from the c:\proj\selenium\code\python\twisted\src\selenium directory even though I set the path statement.
I ran it from the c:\proj\selenium\code\python\twisted\src\samples. Also since I am using IE, I changed the script to run start run_ie.bat. Is IE cool to use or must I use firefox?
Also I copied the following url into an IE browser to attempt to debug things http://localhost:8080/selenium-driver/RPC2#tbend. I did see at the bottom "xml.parsers.expat.ExpatError: no element found: line 1, column 0"
I have been looking at the scripts called but am not sure where else to look to get this simple script to work. I will point it at another page once I get the google one to work. Can you point me in the right direction?

Thank you again.

Unknown said...

Hey Guru,

Hope you are doing well. Guru, we have been using selenium to automate the tests. So far we used "Html" to write the tests. Now we are switching to python and we have written some scripts also but we dont know how to invoke them from selenium-RC server.
This is my svn path:

there we have scripts like

we dont know how to invoke it from selenium-RC.

Could you please help me with that.

Looking forward to your response.


Grig Gheorghiu said...

Sandeep -- I must confess I got a kick out of you calling me a Guru ;-) You made my day.

Anyway, for Selenium RC and Python, check out this very recent blog post by James Gardner:

Hopefully it will give you the info you need. This post of mine is VERY outdated.


Modifying EC2 security groups via AWS Lambda functions

One task that comes up again and again is adding, removing or updating source CIDR blocks in various security groups in an EC2 infrastructur...