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:
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:
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:
Thank you!!
- 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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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) |
