Search code examples
pythonmechanize-python

What's the difference between click and submit in Mechanize?


I am trying to log in to Facebook using Mechanize.

I have written these two different scripts to log in:

Script 1

br = Browser()
br.set_handle_robots(False)

userName = 'My EmailID'
password = 'My Password'
response = br.open('https://www.facebook.com/')
if response.code is 200:
    loginForm = br.forms()[0]
    loginForm.set_value(userName, type='email')
    loginForm.set_value(password, type="password")
    response = loginForm.click(label='Log In')

Script 2

br = Browser()
br.set_handle_robots(False)

userName = 'My EmailID'
password = 'My Password'
response = br.open('https://www.facebook.com/')
if response.code is 200:
    br.select_form(nr=0)
    br.set_value(userName, type='email')
    br.set_value(password, type="password")
    response = br.submit()

The second script logged me on to Facebook, but not the first way.

Even though I click on correct submit button in the first script, why didn't it work?

HTML FORM


Solution

  • Updated: Sorry for the late reply. Here is the way if you want to use click:

    from mechanize import Browser
    br = Browser()
    br.set_handle_robots(False)
    
    userName = ''
    password = ''
    response = br.open('https://www.facebook.com/')
    if response.code is 200:
        loginForm = br.forms()[0]
        loginForm.set_value(userName, type='email')
        loginForm.set_value(password, type="password")
    
        br.select_form(nr=0)
        req = br.click(label='Log In')
        response = br.open(req)