1

I use mechanize to log in, but then after I submit the logging in details and I sign in successfully I'm not sure how to keep the session active and fill in the next form. Could anyone give me some tips?

from mechanize import Browser

br = Browser()

br.open("http://example.com")
br.select_form(nr=0)
br['username'] = 'user'
br['password'] = 'pass'
br.submit()
confused00
  • 2,556
  • 21
  • 39

1 Answers1

1

Session would be still active, just continue using br Browser instance.

Print out the current url and see that you passed the "log in" stage:

print br.geturl()
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Hm, weird. Because if I try to select a form again with br.select_form(nr=0) or by name or even br.select_form(name = "dsadsada") I just get mechanize._form.ParseError: OPTION outside of SELECT – confused00 Aug 15 '14 at 16:35
  • @confused00 you may not be logged in. Check if you see the form in the `source`: `print br.response().read()` – alecxe Aug 15 '14 at 16:37
  • I am, I did check the HTML and the form is there – confused00 Aug 15 '14 at 16:41
  • @confused00 yeah, this is probably the part of a different question. I am afraid, mechanize doesn't like the form markup and refuses to interact with it. – alecxe Aug 15 '14 at 16:42
  • @confused00 see also http://stackoverflow.com/questions/11659268/python-mechanize-select-form-parseerror-option-outside-of-select and http://stackoverflow.com/questions/22568532/python-mechanize-forms-err. – alecxe Aug 15 '14 at 16:42
  • @confused00 alternatively, while you are not stuck with `mechanize` too deeply, give [MechanicalSoup](https://github.com/hickford/MechanicalSoup) a try. – alecxe Aug 15 '14 at 16:44
  • Yup, the BeautifulSoup thing did it. Cheers mate – confused00 Aug 15 '14 at 16:51