July 11, 2011

Python - Getting Started With Selenium WebDriver on Ubuntu/Debian

This is a quick introduction to Selenium WebDriver in Python on Ubuntu/Debian systems.

WebDriver (part of Selenium 2) is a library for automating browsers, and can be used from a variety of language bindings. It allows you to programmatically drive a browser and interact with web elements. It is most often used for test automation, but can be adapted to a variety of web scraping or automation tasks.

To use the WebDriver API in Python, you must first install the Selenium Python bindings. This will give you access to your browser from Python code. The easiest way to install the bindings is via pip.

On Ubuntu/Debian systems, this will install pip (and dependencies) and then install the Selenium Python bindings from PyPI:

 
$ sudo apt-get install python-pip
$ sudo pip install selenium

After the installation, the following code should work:

 
#!/usr/bin/env python

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://www.ubuntu.com/')

This should open a Firefox browser sessions and navigate to http://www.ubuntu.com/

Here is a simple functional test in Python, using Selenium WebDriver and the unittest framework:

 
#!/usr/bin/env python

import unittest
from selenium import webdriver


class TestUbuntuHomepage(unittest.TestCase):
    
    def setUp(self):
        self.browser = webdriver.Firefox()
        
    def testTitle(self):
        self.browser.get('http://www.ubuntu.com/')
        self.assertIn('Ubuntu', self.browser.title)
        
    def tearDown(self):
        self.browser.quit()


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

Output:

 
testTitle (__main__.TestUbuntuHomepage) ... ok

----------------------------------------------------------------------
Ran 1 test in 5.931s

OK

10 comments:

Michael D. Healy said...

Newer to unit testing, and quickly an avid reader of your blog.

Ran into an error with Python 2.6.6 on Ubuntu Maverick:

__init__() got an unexpected keyword argument 'verbosity'

Michael

Corey Goldberg said...

Michael,
ah, the .main() verbosity parameter was added to unittest in Python 2.7 (which I am using on Ubuntu 11.04).

you can just remove that parameter and it should run under 2.6 (with less verbose output).

sorry for confusing you :)

the docs for unittest in 2.6 show how to load and run test cases and enable verbosity if you want more detail:

http://docs.python.org/release/2.6/library/unittest.html

-Corey

Aariff said...

Corey,

i followed your post in stack overflow for the installation of selenium 2 with python. I did downloaded selenium web driver bindings, then i did PIP install selenium and then i downaloaded the .jar file and ran as said in the documentation ( though u mentioned it is not required for webdriver but the selenium and google site suggest that (http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html)). i have a sample program downloaded from ur site but still i am getting "import error: cannot import name webdriver"

Aariff said...

Corey,

this is ref to the above message. i uninstalled and reinstalled everything in order. selenium webdriver started working fine. thanks for the info.

Aariff

Anonymous said...

I guess for Python v2.6 users, assertIn also won't work..
This should work
self.assertTrue('Ubuntu', self.browser.title)

Vineeth said...

New to software testing
OS: Windows XP

1. Installed Python 2.7
2. Installed Eclipse Classic SDK
3. Added PyDev to Eclipse
4. Python interpretter path was set in Eclipse

At this stage, I could run any python programs in this IDE

5. Ran Selenium IDE firefox plugin and recorded a simple test case. Exported as Webdriver-Python script

Trouble starts here.

How do I manage to run the py script in Eclipse, What all things I need to setup for that?, No idea how to bring up selenium python bindings to Eclipse.

Appreciate inputs..

Vineeth said...

Corey, thanks for your inputs in sqaforums, mentioned those below (step 1, 2, 3, 4)

1) install Python
- go to http://python.org/download/
- download the latest MSI installer for Python 2.7 (32-bit)
- run the installer

2) set your PATH
- go to your system's environment variables, and
add 'C:\Python27' to your PATH (this will enable
you to invoke 'python.exe' from any command line)

3) install setuptools for Python
- go to http://pypi.python.org/pypi/setuptools
- download the latest setuptools exe file for Python
2.7 on Windows (currently: setuptools-0.6c11.win32-py2.7.exe)
- run the installer

4) easy_install selenium bindings
- open a command prompt (cmd.exe)
- 'cd c:\Python27\Scripts'
- 'easy_install selenium'
- this should download and install the latest version of selenium bindings from PyPI

5) Installed eclipse classic, integrated PyDev to Eclipse (Help->Install New softwares).

6) Within Eclipse: Window->Preferences->Interpreter-Python -> added python.exe path (adds python and selenium libraries)

Anonymous said...

Hi Corey,

Can I use Python 3.0 with Selenium Webdriver ?

Farz

Corey Goldberg said...

@Farz,
there are some forks around of webdriver that work with python 3, but it's not officially supported yet. hopefully soon.

Unknown said...

Your Selenium Python blogs are immensely helpful. Thank you so much.