1

I have mechanize script that is done logging in. After log in. The page shows a redirect first before going into the main logged in page.

Executing redirect() brings me back to the login page. Why?

Executing login() gives me this page w/c is right but still needs to continue to the main page.

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
     <form name="form1" method="post" action="tmp.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZCOgyU+AdP30f85W4DdUIV6LnCqa" />
</div>

      <script type="text/javascript">
          top.location.href = document.location.href;
          document.forms["form1"].submit();
      </script>
          &nbsp;&nbsp;</form>
      </body>
</html>

I don't really know what to do as for I am new at this.

How do I submit this kind of form using the already authenticated data provided from my first login?

Also how to submit more POST data with the authenticated user?

My code so far:

import re
import mechanize

login_url = 'login.aspx'

def login(id, username, password):
    br = mechanize.Browser()
    br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
    br.open(login_url)
    br.select_form(nr=0)
    br.form.set_all_readonly(False)
    br["__EVENTTARGET"] = "TransactBtn"
    br["AccountID"] = id
    br["UserName"] = username
    br["Password"] = password   
    response = br.submit()
    return response.geturl()
    #after submitting this it goes to the redirect portal page then to the main page

def redirect(url): 
    #after login we submit the redirect portal to see the main page
    br = mechanize.Browser()
    br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
    br.open(url)
    br.select_form(nr=0)
    response = br.submit()
    return response.read() #to the main

def dostuff():
    #this will submit some data as POST with the authenticated user.

print redirect(login('myid', 'myusername', 'mypassword'))
2
  • This already is submitting the form. What is the problem actually? Commented Nov 27, 2012 at 8:09
  • after submitting the form the response.read() is a redirect page. Does this mean its already done though? Also how do I send more data using the authenticated user? Like I want to do a delete this post. And I can only do that If I'm logged in. Can I see a simple example of doing that? Thanks. Commented Nov 27, 2012 at 8:11

2 Answers 2

3

I think you've got this problem because you're creating new instance of mechanise for any request. Mechanise is somewhat like a browser, with cookies storage and so on. And re-creation of it's object is totally like clearing all data in browser.

So, you must share one single instance of Browser class among all your requests.

login function looks like doing what you need, try to print br._ua_handlers['_cookies'].cookiejar to ensure all cookies are set by login handler on server, and then use the same instance of Browser to pull the pages you need.

Best of all I think is to create a class and set Browser it's class variable.

class MyWorker(object):
    def __init__(self):
        self._br = mechanize.Browser()
        self._br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

    def login(self):
        self._br.open(login_url)
        self._br.select_form(nr=0)
        self._br.form.set_all_readonly(False)
        self._br["__EVENTTARGET"] = "TransactBtn"
        self._br["AccountID"] = id
        self._br["UserName"] = username
        self._br["Password"] = password   
        self._br.submit()

I can be wrong, but looks like Javascript here doesn't really matters.

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

1 Comment

Can you elaborate it more? I'm really new to this. Thanks :) Also how do I call login from the class?
2

Mechanize does not support javascript. You should look at Selenium, which does pretty much the same thing as mechanize, but handles javascript.

2 Comments

I tried selenium but I get an error when installing it. My py version is 2.4..how to update to the latest? I've read that selenium dont support the old py.
have you tried to uninstall all about python 2.4 (keep a file with all the modules names you are working with maybe) and install properly python 2.7? might not be the best way, and i'm sorry for that, but could be one solution! Maybe you could take a look at scrapy also. Don't know what version of python it supports though. And it's way longer to get started with scrapy than selenium

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.