Tuesday, March 17, 2015

Selenium WebDriver with Python

In the following blog I will show you how to setup and write your first test case using Webdriver in Python Language.

Install Python:

If you are using Linux or Mac you need not do this as Python comes pre-installed on these OS. however if you are using windows (most of us do), Please follow the steps mentioned blow to install python on windows platform.
  • Download the Python from following location for your windows OS and install it.
  • Once installed you need to set the following paths in your system PATH variable. (Python folder name may be depends on the version of the Python you have installed)
    • C:\Python27
    • C:\Python27\Scripts
  • Now, let's test if Python has installed or not. Open command prompt (CMD) and enter following command, if it shows you version number then python has successfully installed.
    python --version

Now let's Install WebDriver for Python

Installing WebDriver in python is different than installing in Java, actually Installing webdriver for Python is pretty simple.  To install WebDriver, Just open command prompt (CMD) and enter following command. 
  • pip install selenium

First WebDriver Test in Python.

from selenium import webdriver
import unittest

class TestGoogle(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def tearDown(self):

    def test_search(self):
        search_edit_box = self.driver.find_element_by_name("q")
        search_edit_box.send_keys("webdriver with python")

if __name__ == '__main__':

Now let's see How to run Above Webdrive Test

  • First of all copy above file and paste it in any file and save as test_google.py
  • Open command prompt (CMD) and navigate to folder where you have stored this file. 
  • Now run the following command
    python test_google.py