python - Python 机械化检查是否需要字段
问题描述
我正在尝试机械化库来注册用户。有名字,姓氏,电子邮件,密码4个字段。每个字段都是必需的,但如果我运行此代码,它会成功运行并且不会出错,也不会注册用户(缺少姓氏)。在提交表格之前如何检查所有必填字段?
import csv
import mechanize
br = mechanize.Browser()
#br.set_all_readonly(False) # allow everything to be written to
br.set_handle_robots(False) # ignore robots
br.set_handle_refresh(False) # can sometimes hang without this
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36')] # [('User-agent', 'Firefox')]
response = br.open('abc.com/register.aspx')
for form in br.forms():
print ("Form name:", form.name)
br.select_form("TheForm")
br.form['txtEmail$TheBox'] = 'abc@xyz.com';
br.form['txtPass$TheBox'] = 'abcxyz123';
br.form['txtFname$TheBox'] = 'abc';
response = br.submit()
print(response.read())
解决方案
后:
br.select_form("TheForm")
采用:
for control in br.controls: print control
这应该显示所有必填字段。
推荐阅读
- c - C 中 memcpy 在并行 mandelbrot 算法中的分段错误:如何在结构中使用指针?
- javascript - 调整文本大小以适应绘制的圆形 HTML 画布中的切片
- hash - 自定义类型的哈希函数
- c# - 用 C# 读取 XML 并填充 List
- keras - keras fit_generator 从 hdfstore 读取块
- destructuring - Hylang 地图解构
- powershell - 获取证书到期日期powershell
- ruby - 忽略特定的 \n 字符,同时仍然启用 m 标志
- python - Python 3.6.5 中的 Base58 和 utf-8 编码问题
- docker - 离开 docker swarm,但让服务继续运行