1

I am trying to use Mechanize to login to a page and download a file but it doesn't seem to work as expected. This is after I have failed with urllib2 and requests.

import mechanize 

username = '***'
password = '***'
url = 'https://emea1.login.cp.thomsonreuters.net/auth/UI/Login'

br = mechanize.Browser()
br.set_handle_robots(False)
request = br.open(url)

br.select_form(name="frmSignIn")

br['IDToken1'] = username     # Set the form values
br['IDToken2'] = password   # Set the form values
resp = br.submit()

print resp.read()

The output is same as the source code of url, same was the case with requests and urllib

PS: I tried using selenium only to find my corporate server won't have firefox etc. installed.

User
  • 483
  • 4
  • 19
  • I think the page uses JS to login, and `mechanize` isn't a JS engine. thus, I think that you're using the wrong tool. consider using selenium instead. https://selenium-python.readthedocs.org/ – Bee Smears Feb 19 '15 at 23:01
  • @BeeSmears any other solution, I don't have firefox installed on that server I want to run this. – User Feb 19 '15 at 23:12
  • you can use chrome, et al. see http://stackoverflow.com/questions/13724778/how-to-run-selenium-webdriver-test-cases-in-chrome. also there's the second comment on this answer. http://stackoverflow.com/a/5992203/2058922 – Bee Smears Feb 19 '15 at 23:15
  • it's a total pain in the butt learning that one can't submit forms on *some* sites without javascript, but you're not the first -- assuming that's the problem. – Bee Smears Feb 19 '15 at 23:16
  • Can I replicate what javascript is doing here with mechanize? – User Feb 19 '15 at 23:17
  • I've found that one can always mimic JS (or work around it), but it was always too involved for me. wish I could be of greater service. (note that you will likely need to get what I assume is a hash value of your password from the inspector.) – Bee Smears Feb 19 '15 at 23:20
  • every site is different but analysis needs to be done about which url is being hit to authenticate and what is being passed to successfully log in. You would start by analyzing the network panel in chrome, then use something like fiddler to test sending data in the header body etc... to get the desired results, once that's done do the same in requests. Fiddler in my opinion is just quicker with the analysis process. – Chris Hawkes Feb 24 '15 at 15:26

0 Answers0