2

I have been using this guide to help me: http://www.pythonforbeginners.com/cheatsheet/python-mechanize-cheat-sheet/

I want to log into Stack Overflow and print out my login response using Mechanize in Python. I have been troubleshooting to find a form name to select and enter my information into, but am currently getting an error "ValueError: at least one argument must be supplied to specify control" because I am trying to print out all forms and controls to help me figure out what I need to choose from.

   import mechanize

   #make browser object
   browser = mechanize.Browser()

  #open stack login page
  browser.open("http://stackoverflow.com/users/login#login-in")

  #print all the forms on the page
  print("\n Printing all forms on page...\n")
  for form in browser.forms():
          print("Form Name:", form.name)
          print form
  print("\n Done printing forms...\n")

  #printing the controls of each form

  browser.form=list(browser.forms())[0]

  print("\n Printing controls of form 0 \n")
  for control in browser.form.controls:
          print control
          print "type=%s, name=%s, value=%s" % (control.type, control.name, browser[control.name])

  browser.form=list(browser.forms())[1]

  print("\n Printing controls of form 1 \n")
  for control in browser.form.controls:
          print control
          print "type=%s, name=%s, value=%s" % (control.type, control.name, browser[control.name])

  browser.form=list(browser.forms())[2]

  print("\n Printing controls of form 2 \n")
 for control in browser.form.controls:
          print control
          print "type=%s, name=%s, value=%s" % (control.type, control.name, browser[control.name])

 print("\n Done printing all controls \n")

It looks like there are 3 forms on the page, but they do not contain the email and password inputs I am looking for to login. What am I doing wrong?

1 Answer 1

1

Login forms for SO appear to use extensive JavaScript. Note that all of the buttons related to login on the page you posted call JavaScript functions. mechanize can not execute JavaScript and therefore won't show the content you need.

You could drive a browser using Selenium with Python bindings. You could also switch to doing everything in JavaScript using CasperJS.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I think I got it to work with Selenium, just have to do some editing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.