21 Apr

Selenium with Python

Introduction:

Selenium automates web browsers.There are other tools which can be used to automate both web applications and windows applications like QTP (Quick Test Professional). As every one aware that QTP is Licensed Tool where as Selenium is Open source tool, download it configure it and enjoy.

History:

Selenium IDE

In 2005, Selenium first came up with record and play back option known as Selenium IDE. It only comes with Firefox plugin.

Selenium RC

It is the second version of Selenium, where user sends commands to proxy server and then pass data to browser.

Pros:

  • Supports all web browsers unlike Selenium IDE.
  • Supports multiple OS.

Cons:

  • Cannot support Android OS & iOS.
  • Doesn’t support rich API.
  • Need to start proxy server.
  • Cannot talk directly to browser. Instead, proxy server takes commands from user and pass it to browser.

Selenium Webdriver

It is the latest version of Selenium, where it directly talks to browser.Unlike Selenium RC, it doesn’t use any proxy server. It was developed in 2009.

Pros:

  • Handle Mouse movements.
  • Support for Android OS and iOS unlike Selenium RC.
  • Can handle rich API.
  • Support multiple browsers.
  • Supports all the latest versions of Firefox.

Cons:

  • Limitation in Browser Support.
  • There is no support for running tests in parallel on a single computer.

Selenium with Python bindings:

In this blog, we will talk on Selenium with Python bindings.

Let’s start by installing Python2.7. Please follow below link to install Python2.7 and Eclipse editor for creating python project.

http://qaprogrammer.com/python-2/

Use below link to install Selenium with Python bindings.

https://pypi.python.org/pypi/selenium

Webdriver for different browsers:

For Chrome browser,

https://chromedriver.storage.googleapis.com/index.html?path=2.29/

For Firefox browser, no need to download Firefox driver. By default, selenium python bindings comes up with firefox driver.

For Opera browser,

https://github.com/operasoftware/operachromiumdriver/

For Safari browser,

https://webkit.org/blog/6900/webdriver-support-in-safari-10/

Please follow below link for different browsers.

http://www.seleniumhq.org/download/

Sample Program:

Here is the sample selenium program in Python to get inbox message count from Gmail account using Firefox browser.

# -*- coding: utf-8 -*-

”’

    Author: Gandhi Gonugunta

”’

#

#IMPORTS

#

import unittest

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.common.exceptions import NoSuchElementException

import time

import re

import sys

class Gmail_Login(unittest.TestCase):

    

    def setUp(self):

        self.username = raw_input(“Enter Gmail Username : “)

        self.password = raw_input(“Enter Gmail Password : “)

        self.driver = webdriver.Firefox()

    

    def test_gmail_inbox(self):

        driver = self.driver

        driver.get(“http://www.gmail.com”)

        time.sleep(5)

        

        try:

            search = driver.find_element_by_id(“Email”)

            search.clear()

            search.send_keys(self.username)

            driver.find_element_by_id(“next”).click()

        

            time.sleep(3)

            pswd = driver.find_element_by_xpath(“//input[@id=’Passwd’]”)

            pswd.clear()

        

            pswd.send_keys(self.password)

            driver.find_element_by_xpath(“//input[@id=’signIn’]”).click()

        except NoSuchElementException:

            print “Error: Failed to Login to Gmail Account”

            sys.exit()

        

        time.sleep(5)

        try:

            inbox = driver.find_element_by_partial_link_text(“Inbox”)

            inboxdata = inbox.get_attribute(“title”)

            reobj = re.search(r”Inbox \((.*)\)”,inboxdata)

            if reobj:

                print “##################################”

                print “You have %s inbox messages” %str(reobj.group(1))

                print “##################################”

            else:

                print “Failed to get your inbox messages”

        except NoSuchElementException:

            print “Error: Failed to Login to Gmail Account”

            sys.exit()

        try:

            driver.find_element_by_xpath(“.//*[@id=’gb’]/div[1]/div[1]/div[2]/div[4]/div[1]/a/span”).click()

            time.sleep(2)

            driver.find_element_by_xpath(“.//*[@id=’gb’]/div[1]/div[1]/div[2]/div[4]/div[2]/div[3]/div[1]/a”).click()

            time.sleep(3)

            print “Successfully Logged out from Gmail”

            print “Done”

        except NoSuchElementException:

            print “Error: Failed to Logout from Gmail Account”

            sys.exit()

    

    def tearDown(self):

        self.driver.quit()

        pass

if __name__ == “__main__”:

    unittest.main()

Share this

Leave a reply