1

so that this way all I have to do is type

browser = MyBrowser()
browser.login()

to get my python scripts to log in in the future. Here's what I have so far:

import mechanize
class MyBrowser(mechanize.Browser, object):
    _username = 'username'
    _password = 'password'

    def __init__(self):
        super(MyBrowser, self).__init__()
        self.set_handle_robots(False)
        self.set_proxies({"http" : "http://proxy.me.com:80"})

    def login(self):
        self.open('http://login.mypage.com/')
        self.select_form(nr=0)
        self['name'] = self._username
        self['pass'] = self._password
        self.submit()

I had made a login function that worked fine using this same methodology. But now, when I call browser.login(), I get this:

self['name'] = self._username
TypeError: 'MyBrowser' object does not support item assignment

Why doesn't calling select_form behave the same way when it's part of a method like this?

Andy
  • 65
  • 11

2 Answers2

1

I got it!!!

self.select_form(nr=0)
self.form['name'] = self._username
self.form['pass'] = self._password
self.submit()

Thanks so much Lior, I really appreciate your time on this and couldn't have gotten it without your help :)

Andy
  • 65
  • 11
0

self is an object of the MyBrowser class. What you have done with the self['name'] and self['pass'] is a try to assign values to the self object itself.

I assume you wanted to declare variables in the class, in order to do that you have to edit your code to:

self.name = self._username
self.pass = self._password

Edit: Try this one:

super(MyBrowser, self).name = self._username
super(MyBrowser, self).password = self._password
Lior
  • 5,841
  • 9
  • 32
  • 46
  • Thanks so much for your answer! Unfortunately this doesn't work. What I'm trying to do is get it to assign those values to the form that should be a part of the `self` object after I assign the form to it using `self.select_form(nr=0)` I based this on the model found here: [Download login-protected pages with Python using Mechanize and Splinter (Part 3)](http://ubuntuincident.wordpress.com/2011/11/08/download-login-protected-pages-with-python-using-mechanize-and-splinter-part-3/) – Andy Dec 05 '12 at 14:43
  • Read that thread for more information: http://stackoverflow.com/questions/1021464/python-how-to-call-a-property-of-the-base-class-if-this-property-is-being-overw – Lior Dec 05 '12 at 17:26
  • Thanks! I just realized what was actually causing the error with your original answer though: `self.pass = self._password` pass is a reserved word, and it won't let me use it as a field name, but that's what it is in the form! :( I'm looking for ways to escape reserved words... hmm – Andy Dec 05 '12 at 19:13