pythonpython-3.xmechanizemechanize-python

Python mechanize check if field is required


I am trying mechanize library to register a user. There are 4 fields firstname, lastname, email, password. Every field is required but If I run this code it runs successfully and doesn't give error also doesn't register a user(last name is missing). How can I check all the required fields before submitting the form?

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())


Solution

  • after:

    br.select_form("TheForm")
    

    use:

    for control in br.controls: print control
    

    This should show all required fields.