python-mechanize / mechanize

The official source code for the python-mechanize project
BSD 3-Clause "New" or "Revised" License
728 stars 108 forks source link

Multi-page login, can't find the form of the second page. #55

Open ronin1770 opened 4 years ago

ronin1770 commented 4 years ago

Mechanize Browser object is unable to find form on the second page of the login process. Internal data structure of the browser object seems to hold information about the original form.

Here are the chain of events in the multi-stage login process:

  1. User submits his username / password on the first page (let's call it P1)
  2. This information is validated by the website and user is redirected to the second page (let's call it P2)
  3. On P2 user is required to submit answer to the security question.
  4. However, when user tries to find the form / control for the security answer, Mechanize complains the form /control is not found.
  5. If response.text is printed for P2 - it displays the HTML of the P2 correctly

Sample code is shown below:

browser = mechanize.Browser(history=None)

browser.open(LOGIN_URL)
browser.select_form(nr = 0)
browser.form['username'] = UID
browser.form['userpass'] = PWD
resp = browser.submit()

security_url =  resp.geturl()

print( "The security url is: " + str(security_url) )

resp1 = browser.follow_link(security_url)
forms =  list(browser.forms())

for i in range(len(forms)):
    print( forms[i].name )
kovidgoyal commented 4 years ago

You will need to provide a minimal example where this fails.

ronin1770 commented 4 years ago

browser = mechanize.Browser(history=None)

browser.open(LOGIN_URL)
browser.select_form(nr = 0)
browser.form['username'] = UID
browser.form['userpass'] = PWD
resp = browser.submit()

security_url =  resp.geturl()

print( "The security url is: " + str(security_url) )     #Security URL is security challenge URL

resp1 = browser.follow_link(security_url)              #Response correctly shows HTML from challenge URL page 
forms =  list(browser.forms())

for i in range(len(forms)):
    print( forms[i].name )  #Form[0] -  must contain a control called 'verify' it doesn't

browser.select_form(    nr = 0)
browser.form['verify'] = SECRET
resp = browser.submit()

print( resp.getcode() )
print( resp.read() )
kovidgoyal commented 4 years ago

That's useless without LOGIN_URL and SECRET

Karmabecu commented 3 years ago

The Login url is https://m.facebook.com/profile.php?id=100002233045625&tsid=0.48424488103504615&source=result Can you help