Selenium Webdriver With Python

Basic Example

import unittest
from selenium import webdriver

class Login(unittest.TestCase):
    def setUp(self):
       self.driver = webdriver.Firefox()

    def tearDown(self):
       self.driver.quit()

    def test_login(self):
       driver = self.driver
       driver.get('http://testurl')
       username = driver.find_element_by_name('user_id')
       username.send_keys('admin')
 
 

Selecting element from drop down list

el = driver.find_element_by_id('id_line')
for option in el.find_elements_by_tag_name('option'):
    if option.text == "line to select":
        option.click()
        break


Verifying Table Values


#Importing selenium webdriver library 
from selenium import webdriver

#Creating an driver instance of Firefox browser
driver = webdriver.Firefox()  
#Accessing the Flipkart site url for the python book

driver.get("http://www.flipkart.com/programming-python-4th-e-d/p/itmczzj4gpfrr6bs?pid=9789350232873&icmpid=reco_pp_hSame_book_1")
#creating an array object named data
data=[]

 #Iterating over the table data using xpath to verify the price
for tr in driver.find_elements_by_xpath('//table[@class="mprod-similar-prod-table"]//tr'):
  tds=tr.find_elements_by_tag_name('td')
  if tds:
        data= [td.text for td in tds]
print "3730" in data[2] #or whatever is the current price

Comments

Popular posts from this blog

Testing Webservice with SoapUI Having Windows Authentication

How to Re-run Failed Scenarios in Cucumber

Data Driven Testing Using Selenium -WebDriver in Python