Home > OS >  Use loop to automated filling forms from a csv: loop repeats before form submission
Use loop to automated filling forms from a csv: loop repeats before form submission

Time:02-21

I have a script that automatically fills a web form with data from csv using a loop function. My problem is that the script loops before submitting the form. So all of the row data for each column is pasted into each corresponding form field instead of pasting in a single row's data for each form field, submitting, and then looping to the next row.

I assume the issue is that the website requires users to confirm the address from a list of options before submission is finalized (usually the first address selected from a list of options is the correct one). If this is not done, the submission fails and the loop pastes in data from the next row.

Is it possible for the script to have a 20 second delay before looping so that I can review the address options field before the script submits the form, and then loops again for the next row of data? Or even better, I would like the script to automatically confirm the address from the first list of address options and then submit.

Thank you all!

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import csv
import time
import pandas as pd

# import csv file
table = pd.read_csv('...test2.csv')

print(table)

address1 = table['Address2'].tolist()
unit1 = table['Unit2'].tolist()
unittype1 = table['Unit Type'].tolist()
beds1 = table['Beds2'].tolist()
bath1 = table['Baths2'].tolist()
rent1 = table['Rent2'].tolist()

# open chrome
# driver = Webdriver.chrome("C:\Python Tools\chromedriver.exe")
s = Service("C:\Python Tools\chromedriver.exe")
driver = webdriver.Chrome(service=s)

# Enter login
driver.get("https://hadashboard.gosection8.com/pages/login/Login.aspx")
driver.implicitly_wait(5)
driver.find_element(By.CSS_SELECTOR, ".form > input:nth-child(3)").send_keys("hiddenlogin")
driver.find_element(By.CSS_SELECTOR, ".form > input:nth-child(6)").send_keys("hiddenpw")
driver.find_element(By.CSS_SELECTOR, ".m-col-12:nth-child(8)").click()
driver.implicitly_wait(10)
# go to rent reasonableness analysis
driver.find_element(By.CSS_SELECTOR, ".not-now-btn").click()
driver.find_element(By.CSS_SELECTOR, ".clear-fix > div > .rent-btn-row > .primary-button").click()
driver.implicitly_wait(10)



# template code for loop https://stackoverflow.com/questions/66933061/looping-through-several-columns-and-rows-from-csv-to-fill-a-form

    
address = driver.find_element(By.ID, "SubjectPage_AutocompleteAddress")
unit = driver.find_element(By.ID, 'SubjectPage_AddressLine2_Auto')
beds = driver.find_element(By.ID, "SubjectPage_BedroomCount")
baths = driver.find_element(By.ID, "SubjectPage_FullBathCount")
rent = driver.find_element(By.ID, "SubjectPage_AskingRent")
# unittype fix later!



for address1, unit1, unittype1, beds1, bath1, rent1 in zip(address1, unit1, unittype1, beds1, bath1, rent1):
    
    address.send_keys(address1)
    driver.implicitly_wait(10)
    
    unit.send_keys(unit1)
    driver.implicitly_wait(10)
    
    #unit.send_keys(unittype)
    #driver.implicitly_wait(10)
    
    beds.send_keys(beds1)
    driver.implicitly_wait(10)
    
    baths.send_keys(bath1)
    driver.implicitly_wait(10)
    
    rent.send_keys(rent1)
    driver.implicitly_wait(10)
    
    driver.find_element(By.CSS_SELECTOR, "#SubjectPage_PropertyType_Fake > select").click()
    dropdown = driver.find_element(By.CSS_SELECTOR, "#SubjectPage_PropertyType_Fake > select")
    dropdown.find_element(By.XPATH, "//option[. = 'Apartment']").click()
    submit = driver.find_element(By.ID, "SubjectPage_AnalyzeBottom").click()

CodePudding user response:

You can use the python sleep() function to achieve a delay in seconds before the loop starts. Be careful with this because it is not running based on a condition except time. If you have a large data or if you experience delay in form processing this timer will still cause the sleep function to time out and the loop will begin running. I will suggest you set a condition to be true before running the loop. For example, set a variable to True if the form is completed. Then while that condition remains true run the loop. But the loop will need to set the variable's value to false when it is done looping and loading the data.

  • Related