All Xpath values are entered in column E of Excel.
Find the Xpath in column E and enter the value entered in column D into Chrome.
How can I make this code into a loop.
load_wb = load_workbook(filename, data_only=True)
load_ws = load_wb['apple']
elem=driver.find_element_by_xpath(load_ws['E2'].value)
elem.send_keys(Keys.CONTROL 'a',Keys.DELETE)
elem.send_keys(load_ws['D2'].value)
elem=driver.find_element_by_xpath(load_ws['E3'].value)
elem.send_keys(Keys.CONTROL 'a',Keys.DELETE)
elem.send_keys(load_ws['D3'].value)
elem=driver.find_element_by_xpath(load_ws['E4'].value)
elem.send_keys(Keys.CONTROL 'a',Keys.DELETE)
elem.send_keys(load_ws['D4'].value)
elem=driver.find_element_by_xpath(load_ws['E5'].value)
elem.send_keys(Keys.CONTROL 'a',Keys.DELETE)
elem.send_keys(load_ws['D5'].value)
elem=driver.find_element_by_xpath(load_ws['E6'].value)
elem.send_keys(Keys.CONTROL 'a',Keys.DELETE)
elem.send_keys(load_ws['D6'].value)
elem=driver.find_element_by_xpath(load_ws['E7'].value)
elem.send_keys(Keys.CONTROL 'a',Keys.DELETE)
elem.send_keys(load_ws['D7'].value)
elem=driver.find_element_by_xpath(load_ws['E8'].value)
elem.send_keys(Keys.CONTROL 'a',Keys.DELETE)
elem.send_keys(load_ws['D8'].value)`
CodePudding user response:
In case there are 7 rows in the Excel table your code can be simply rewritten as following:
load_wb = load_workbook(filename, data_only=True)
load_ws = load_wb['apple']
for i in range(2,9):
elem=driver.find_element_by_xpath(load_ws['E' str(i)].value)
elem.send_keys(Keys.CONTROL 'a',Keys.DELETE)
elem.send_keys(load_ws['D' str(i)].value)
In case the amount of rows is different or can be changing you will have to change the range used in the for loop accordingly.
CodePudding user response:
createa for loop using range , and then call i using format :
load_wb = load_workbook(filename, data_only=True)
load_ws = load_wb['apple']
for i in range(2,9):
elem=driver.find_element_by_xpath(load_ws[f'E{i}'].value)
elem.send_keys(Keys.CONTROL 'a',Keys.DELETE)
elem.send_keys(load_ws[f'D{i}'].value)