Reputation: 41
I've researched on how to loop in selenium but I haven't seen any answers that worked for me. Basically, this will be the steps that needs to be followed on my code:
- Get data row by row from the csv file and feed it to the web app
- First data in row 1 will be 1 cycle of the selenium test and save the data inside web app
- Selenium will loop again and go to the 2nd row of data in the csv file and save it again and loop again until all data in the csv are saved inside web app.
This code works but it just stops on the 1st row of data and won't proceed to the 2nd row of data and so on so forth.
driver = webdriver.Firefox()
driver.get("https://*****/")
file = open("testfile.csv")
reader = csv.DictReader(file)
data = [row for row in reader]
for row in data:
Name = (row["Name"])
Eadd = (row["Eadd"])
time.sleep(10)
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Name'])[1]/following::input[1]").click()
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Name'])[1]/following::input[1]").send_keys(Name)
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Email'])[1]/following::textarea[1]").click()
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Email'])[1]/following::textarea[1]").clear()
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Email'])[1]/following::textarea[1]").send_keys(Eadd)
Upvotes: 0
Views: 55
Reputation: 14145
Here is the logic that you have to implement.
driver = webdriver.Firefox()
driver.get("https://*****/")
file = open("testfile.csv")
data = csv.reader(file) #<== if you have a `,` separated csv then you can use below line rather this.
# data = = csv.reader(csvfile, delimiter=',')
for row in data:
Name = (row["Name"])
Eadd = (row["Eadd"])
# use the below if `,` seperated
#Name = row[0]
#Eadd = row[1]
time.sleep(10)
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Name'])[1]/following::input[1]").click()
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Name'])[1]/following::input[1]").send_keys(Name)
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Email'])[1]/following::textarea[1]").click()
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Email'])[1]/following::textarea[1]").clear()
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Email'])[1]/following::textarea[1]").send_keys(Eadd)
Upvotes: 1