Python 机械化检查是否需要字段

Python mechanize check if field is required

我正在尝试机械化库来注册用户。有 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

这应该会显示所有必填字段。