Using Excel and Selenium


from selenium import webdriver
import unittest, time
import xlrd

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


      def test_Login(self):
          driver=self.driver
          driver.get(_url_of_the_webpage)
          driver.maximize_window()
          
          
          wb=xlrd.open_workbook('_location_of_the_file')
          sheetname = wb.sheet_names()
         
          #Read for XLSX Sheet names
          sh1 = wb.sheet_by_index(0)

          i=0
          while (i<2):
                rownum=(i)
                rows = sh1.row_values(rownum)
                element = driver.find_element_by_id("gbqfq")
                driver.find_element_by_id("gbqfq").send_keys(rows[0])
                element.submit()
                time.sleep(3)
                print "The keyword [" + rows[0] + "] is searched"
                driver.back()
                time.sleep(3)
                i=i+1

      def tearDown(self):
           self.driver.quit()
 
if __name__ == "__main__":
unittest.main()

4 thoughts on “Using Excel and Selenium

  1. hello i have a doubt in this part
    from selenium import webdriver
    import unittest
    import time
    import xlrd

    class moduleName(unittest.TestCase):
    def setUp(self):
    self.driver = webdriver.Firefox()
    self.driver.implicitly_wait(5)
    self.base_url = “https://www.facebook.com/”
    self.verificationErrors = []
    self.accept_next_alert = True

    def test_Login(self):
    driver = self.driver
    driver.get(self.base_url + “/”)
    #driver.maximize_window()
    # define the path to the Excel file
    wb = xlrd.open_workbook(‘C://Github//python_testing_basics//DDT//data.xls’)
    sheetname = wb.sheet_names()
    # Read for XLSX Sheet names
    sh1 = wb.sheet_by_index(0)
    #sh1.cell_value(0, 0)

    i=0
    while i<6:
    rownum=i
    rows=sh1.row_values(rownum)

    element = driver.find_element_by_name("email").send_keys(rows[0])
    element1 = driver.find_element_by_name("pass").send_keys(rows[0])

    element2 = driver.find_element_by_id("loginbutton").click()
    time.sleep(3)
    print ("The keyword [" + rows[0] + "] is not logged")
    driver.back()
    time.sleep(3)
    i = i + 1

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

    if __name__ == "__main__":
    unittest.main()

    its not working properly i want both email n password in the excel sheet ..plz help

    Like

      1. if you see the above code, he is using the same i value into user name and password. He needs to enter user name and password both into different fields, how to do that?? Is there a way to have Row and Column combination (like row1 column 2) to achieve that.

        Like

  2. Hello, do you have any tutorial where we are filling a webform and picking all values from a excel sheet one by one ?? with python selenium unittest framework ??

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.