Corey Goldberg is a software developer from Boston. His interests include Python, Linux, Open Source, and Testing. Corey is a DZone MVB and is not an employee of DZone and has posted 31 posts at DZone. You can read more from them at their website. View Full User Profile

Python - Getting Started With Selenium WebDriver on Ubuntu/Debian

07.01.2012
| 17036 views |
  • submit to reddit

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
Published at DZone with permission of Corey Goldberg, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Kamal Ahmed replied on Wed, 2012/08/15 - 1:38pm

is there a way we can stop the browser from starting and use something like HtmlUnitDriver ?

Since mostly i login to the linux machine via ssh, and then run these tests remotely. 

Thanks, 

Song Chanho replied on Mon, 2012/09/24 - 4:02am

Hi. ^^

I have a question.

I want to use firefox driver.

but, when webdriver.Firefox() called then error occuerd.

I guess that my linux server didn't install firefox. Is that reason?

Help me please.

File "test.py", line 4, in <module>
    browser = webdriver.Firefox()
  File "/usr/local/lib/python2.7/site-packages/selenium-2.25.0-py2.7.egg/selenium/webdriver/firefox/webdriver.py", line 51, in __init__
    self.binary, timeout),
  File "/usr/local/lib/python2.7/site-packages/selenium-2.25.0-py2.7.egg/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
    self.binary.launch_browser(self.profile)
  File "/usr/local/lib/python2.7/site-packages/selenium-2.25.0-py2.7.egg/selenium/webdriver/firefox/firefox_binary.py", line 43, in launch_browser
    self._start_from_profile_path(self.profile.path)
  File "/usr/local/lib/python2.7/site-packages/selenium-2.25.0-py2.7.egg/selenium/webdriver/firefox/firefox_binary.py", line 65, in _start_from_profile_path
    env=self._firefox_env).communicate()
  File "/usr/local/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/local/lib/python2.7/subprocess.py", line 1228, in _execute_child
    raise child_exception

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.