使用循环从 csv 自动填写表单:循环在表单提交之前重复

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

我有一个脚本可以使用循环函数自动用来自 csv 的数据填充 Web 表单。 我的问题是脚本在提交表单之前循环。因此,每一列的所有行数据都被粘贴到每个相应的表单域中,而不是粘贴到每个表单域的单个行的数据中,提交,然后循环到下一行。

我假设问题是网站要求用户在提交完成之前从选项列表中确认地址(通常从选项列表中选择的第一个地址是正确的)。如果不这样做,提交将失败并且循环从下一行粘贴数据。

脚本是否可以在循环之前有 20 秒的延迟,以便我可以在脚本提交表单之前查看地址选项字段,然后再次循环获取下一行数据?或者更好的是,我希望脚本自动确认第一个地址选项列表中的地址,然后提交。

谢谢大家!

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 

    
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()

您可以使用python sleep() 函数在循环开始前实现以秒为单位的延迟。请注意这一点,因为它不是 运行ning 基于时间以外的条件。如果您有大量数据,或者如果您在表单处理中遇到延迟,此计时器仍会导致睡眠功能超时并且循环将开始 运行ning。我建议您在 运行 循环之前将条件设置为真。例如,如果表单已完成,则将变量设置为 True。然后,当该条件保持为真时 运行 循环。但是循环在完成循环和加载数据后需要将变量的值设置为 false。