【问题标题】:Python mechanize check if field is requiredPython 机械化检查是否需要字段
【发布时间】:2020-07-18 14:04:27
【问题描述】:

我正在尝试使用机械化库来注册用户。有名字,姓氏,电子邮件,密码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())

【问题讨论】:

    标签: python python-3.x mechanize mechanize-python


    【解决方案1】:

    之后:

    br.select_form("TheForm")
    

    使用:

    for control in br.controls: print control
    

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

    【讨论】:

    • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
    猜你喜欢
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    相关资源
    最近更新 更多