当前位置:嗨网首页>书籍在线阅读

35-第5步_提交表单并等待

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

第5步:提交表单并等待

可以用函数 write() 填写Additional Comments输入框,将 person['comments'] 作为参数。你可以另外输入 '\t' ,将焦点移到下一个输入框或Submit按钮。当Submit按钮获得焦点后,调用 pyautogui.press('enter') ,模拟按回车键,提交表单。在提交表单之后,程序将等待5秒,等待下一页加载。

在新页面加载之后,它会有一个Submit another response链接,让浏览器转向一个新的、全空的表单页面。在第2步,你已将这个链接的坐标作为元组保存在 submitAnotherLink 中,所以将这些坐标传递给 pyautogui.click() ,单击这个链接。

新的表单准备好后,脚本的外层 for 循环将继续下一次迭代,在表单中输入下一个人的信息。

添加以下代码,完成你的程序:

#! python3
# formFiller.py - Automatically fills in the form.
--snip--
 # Fill out the Additional Comments field.
 pyautogui.write(person['comments'] + '\t')
 # "Click" Submit button by pressing Enter.
 time.sleep(0.5) # Wait for the button to activate.
 pyautogui.press('enter')
 # Wait until form page has loaded.
 print('Submitted form.')
 time.sleep(5)
 # Click the Submit another response link.
 pyautogui.click(submitAnotherLink[0], submitAnotherLink[1])

在主 for 循环完成后,程序应该已经插入了每个人的信息。在这个例子中,只有4个人要输入。但如果有4000个人,那么编程来完成这个任务将节省大量的输入时间。