There is no such thing as Python webdriver. Webdriver is a component for driving webpages. It has been integrated to the Selenium 2. Natively it works in Java, but there are bindings available for many languages, including Python.
Here's an annotated example from the webdriver documentation modified a little. For creating a unittest, make a test class that inherits the class TestCase provided by unittest module.
#!/usr/bin/python
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import unittest
class GoogleTest(unittest.TestCase):
def test_basic_search(self):
# Create a new instance of the Firefox driver
driver = webdriver.Firefox()
driver.implicitly_wait(10)
# go to the google home page
driver.get("http://www.google.com")
# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_name("q")
# type in the search
inputElement.send_keys("Cheese!")
# submit the form (although google automatically searches
# now without submitting)
inputElement.submit()
# the page is ajaxy so the title is originally this:
original_title = driver.title
try:
# we have to wait for the page to refresh, the last thing
# that seems to be updated is the title
WebDriverWait(driver, 10).until(lambda driver :
driver.title != original_title)
self.assertIn("cheese!", driver.title.lower())
# You should see "cheese! - Google Search"
print driver.title
finally:
driver.quit()
if __name__ == '__main__':
unittest.main()
One nice thing about webdriver is that you can change the driver line to be
driver = webdriver.Chrome()
driver = webdriver.Firefox()
driver = webdriver.Ie()
depending on what browsers you need to test. In addition to ChromeDriver, FirefoxDriver or InternetExplorerDriver there's also HtmlUnitDriver which is most lightweight and can run headless (but may run some javascript differently than browsers), RemoteWebDriver which allows running tests on remote machines and in parallel, and many others (iPhone, Android, Safari, Opera).
Running it can be done as running any python script. Either just with:
python <script_name.py>
or including the interpreter name on the first line like !#/usr/bin/python above. The last two lines
if __name__ == '__main__':
unittest.main()
make the script run the test when this file is run directly like ./selenium_test.py. It is also possible to collect test cases automatically from multiple files and run them together (see unittest documentation). Another way running tests in some module or some individual test is
python -m unittest selenium_test.GoogleTest