Python Forum
Running a selenium test using data from spreadsheet (have problem iterating...)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running a selenium test using data from spreadsheet (have problem iterating...)
#1
Hi there, I'm having an issue with some logic and how the best way to go about it. I am starting to build a selenium framework and what my aim is, is to:
  • Read in the rows from a spreadsheet - the data in each row will be used for one "iteration" or test.
  • Run the selenium test - enter data from spreadsheet from first row into text fields etc. This will be UnitTest later but just dry python code at the mo.
  • Close the test, select second row of spreadsheet and run test again.
I have managed to read the data in a list of dictionaries okay. So each element of the list represents one row. So far, so good.

The problem I'm having is iterating over the list to select row one i.e. url = data[1]['URL'], running the selenium test i.e. driver.get(url), then selecting row 2 i.e. url = data[2]['URL'].

If I put the above in a for loop, literally all my selenium code would have to be in the loop too. I did a quick test to see if the loop would work (see "loop through the list to test it works" comment in first code block under this post), which it does, but I need a method of changing the number of the element in the array e.g. data[3], without in being inside the loop. All of my selenium code is in another class! I must be going about it the wrong way but can't for the life of me figure out how to do it.

If I leave the loop to run, come out of it and run my selenium code, all I'm left with is the last value in the loop - obviously e.g. data[3], if I have 3 rows in spreadsheet.

Please can someone help! I know this may be a confusing post but literally any help would be appreciated. It must involve a loop somewhere but I'm new to python and just can't figure out the logic.

Below is my code that I'm working with:

#readsheet.py
from xlrd import open_workbook
book = open_workbook('test_sheet.xls')
sheet = book.sheet_by_index(0)

"""Read data into a list of dictionaries"""
first_row = [] # The row where we store the name of the column
for col in range(sheet.ncols):
   first_row.append(sheet.cell_value(0,col))
# transform the workbook to a list of dictionaries
data =[]
for row in range(1, sheet.nrows):
   data_dict = {}
   for col in range(sheet.ncols):
       data_dict[first_row[col]]=sheet.cell_value(row, col)
   data.append(data_dict)
#print data
#print data[0]['URL']
url = data[2]['URL']
"""print out cell value 'URL' from row 2"""
print "test: " + url

"""loop through the list to test it works"""
for i in range(len(data)):
   url_value = data[i]['URL']
   password_value = data[i]['password']
   username_value = data[i]['username']
   print url_value
   print password_value
   print username_value
Then in another class I would have my selenium test. The data in variable "url" is from readsheet.py and is what I need to change/update to the data in row 2 after row 1 has finished:

from read_sheet import *

class selenium_class():

   def setUp(self):
       self.driver = webdriver.Chrome('/home/daniel/chromedriver')

   def go_to_url(self, url_value):
       driver = self.driver
       driver.get(url_value)
       driver.implicitly_wait(10)

test_selenium_object=selenium_class()
test_selenium_object.setUp()
test_selenium_object.go_to_url(url)
Thank you!! Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with Selenium webdriver Fred 1 2,036 Jan-10-2022, 05:45 PM
Last Post: Larz60+
  Python Selenium (Dropdown-) data Robin_at_Cantelli 2 6,198 Dec-29-2021, 03:16 AM
Last Post: ondreweil
  Extract data with Selenium and BeautifulSoup nestor 3 3,909 Jun-06-2020, 01:34 AM
Last Post: Larz60+
  Clicking on element not triggering event in Selenium Python (Event Key is not in data dkaeloredo 2 4,270 Feb-16-2020, 05:50 AM
Last Post: dkaeloredo
  error when running headless selenium julio2000 2 4,553 Feb-01-2020, 12:41 PM
Last Post: julio2000
  Selenium get data from newly accessed page hoff1022 2 2,937 Oct-09-2019, 06:52 PM
Last Post: hoff1022
  How to Caputre Data After Selenium Scroll ahmedwaqas92 3 7,079 Aug-18-2019, 12:43 PM
Last Post: ahmedwaqas92
  Unable to access javaScript generated data with selenium and headless FireFox. pjn4 0 2,541 Aug-04-2019, 11:10 AM
Last Post: pjn4
  Selenium Webdriver Memory Problem? satbir129 2 6,874 Mar-01-2019, 04:17 AM
Last Post: satbir129
  Selenium Web Driver Exe not running in other PC's Utkarsh29 2 4,470 Feb-28-2019, 05:48 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020