2

I have the following code:

import requests
import sys
import urllib2
import re
import mechanize
import cookielib
#import json
#import imp
#print(imp.find_module("requests"))
#print(requests.__file__)
EMAIL = "******"
PASSWORD = "*******"

URL = 'https://www.imleagues.com/Login.aspx'
address = "http://www.imleagues.com/School/Team/Home.aspx?Team=27d6c31187314397b00293fb0cfbc79a"
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

br.add_password(URL, EMAIL, PASSWORD)
br.open(URL)

#br.open(URL)
#br.select_form(name="aspnetForm")
#br.form["ctl00$ContentPlaceHolder1$inUserName"] = EMAIL
#br.form["ctl00$ContentPlaceHolder1$inPassword"] = PASSWORD
#response = br.submit()
#br= mechanize.Browser()
site = br.open(address)

    # Start a session so we can have persistant cookies
#session = requests.Session()

    # This is the form data that the page sends when logging in
#login_data = {
 #   'ctl00$ContentPlaceHolder1$inUserName': EMAIL,
 #   'ctl00$ContentPlaceHolder1$inPassword': PASSWORD,
  #  'aspnetFrom': 'http://www.imleagues.com/Members/Home.aspx',
#}
#URL_post = 'http://www.imleagues.com/Members/Home.aspx'
    # Authenticate
#r = session.post(URL, data=login_data)

    # Try accessing a page that requires you to be logged in
#r = session.get('http://www.imleagues.com/School/Team/Home.aspx?Team=27d6c31187314397b00293fb0cfbc79a')



website = site.read()


f = open('crypt.txt', 'wb')
f.write(website)

#print(website_html)

I am trying to log into this site to monitor game times and make sure they aren't changed on me (again). I've tried various ways to do this, most commented out above, but all of them redirect me back to the login page. Any ideas? Thanks.

Hollis Scriber
  • 159
  • 1
  • 8
  • 18

1 Answers1

3

As I see in given website login button is not in submit tag. Login is javascript function

<a ... href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$btnLogin','')" </a>

and mechanize cannot handle javascript. I faced very similiar problem and came up with solution to use Spynner. It is headless web browser. So you can acomplish same tasks as you use mechanize and it has javascript support.

Zygimantas Gatelis
  • 1,923
  • 2
  • 18
  • 27
  • That makes so much more sense. Thanks. – Hollis Scriber Mar 25 '13 at 21:58
  • Any ideas on how to select a choice from a drop down button (javascript)? I've figured out how to fill in my username and password, but I need to select my school now. – Hollis Scriber Mar 26 '13 at 00:58
  • You can run js code in spinner, I guess it easier than do it in python. It looks something like that: `browser.runjs('$("[schid=e002364c7b414cfd8195be06fb7707ba]").click()');` `schid` is attribute of particular school `li` item in `html`. Here is gist i have tried: https://gist.github.com/zygisx/5243735 – Zygimantas Gatelis Mar 26 '13 at 07:44
  • I didn't mean to down-vote this (actually I didn't even realize I did), but now I cannot undo it. It actually answered my question perfectly. Sorry. – slbass Jul 09 '14 at 23:27