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



我有一个脚本,它使用循环函数自动用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 Toolschromedriver.exe")
s = Service("C:Python Toolschromedriver.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()

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

最新更新