0

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:

  1. Get data row by row from the csv file and feed it to the web app
  2. First data in row 1 will be 1 cycle of the selenium test and save the data inside web app
  3. 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)

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, the problem it is putting the same name and eadd over and over
is it a , or | delimited file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.