0

I'm trying to log on onto a website with mechanize for python; however when presented with correct credentials in the form it fails to log in.

Here's what I'm doing following the mechanize tutorial:

def logon(self):
    baseurl = "https://www.website.com/login"
    br = mechanize.Browser(factory=mechanize.RobustFactory())
    br.open(baseurl)
    br.select_form(nr=0)
    br.form["username"] = self.username
    br.form["password"] = self.password
    response = br.submit()
    print response.geturl()

That should take me to the website.com/dashboard but it's saying at the website.com/login page as though it failed. I can log in manually, and everything passed through the debugger so I'm not sure what I'm doing wrong. I did some digging, and I checked through the form HTML for javascript, and the form doesn't seem to have any attached to it.

Here's the form:

<form method="post" class="login-form">
                <fieldset>
                    <div class="row form-section">
                        <label for="email">Email Address</label>
                        <span class="input-text"><input class="required-email" type="text" id="email" value="" name="username" /><span class="required">Required Info</span></span>
                        <!--<input type="text" id="email" name="username" placeholder="Enter Email Address" class="text" aria-required="true" required="required" style='float:right;' size="30" value="" /><p>  -->
                    </div>
                    <div class="row form-section">
                        <label for="pass">Password</label>
                        <span class="input-text"><input class="required-text" type="password" id="pass" value="" name="password" /><span class="required">Required Info</span></span>
                        <!--<input type="password" placeholder="Enter Password" class="text" size="30" autocomplete="off"  value="" id="password" name="password" style='float:right;' aria-required="true" required="required" />-->
                    </div>
                    <div class="row row-btn">
                        <input type="submit" value="Log in" />
                        <span><a href="/sign-up" class="sign-in">Don't have an account?</a><br><br><a href="/component/com_oms/task,reset-password/view,user/" class="sign-in">Forgot your password?</a>&nbsp;&nbsp;<a href="/resend-activation" class="sign-in">Didn't get your activation email?</a></span>
                    </div>
                </fieldset>
                <input type="hidden" value="com_oms" name="option">
                <input type="hidden" value="login" name="task">
                <input type="hidden" name="return" value="">
                <input type="hidden" name="9402951e32acb8d73d4a6972ae540c63" value="1" />
</form>

I know there were some similar questions asked but there haven't seemed to be any clear answers that my searching has brought up. Can anyone shed some light on what is going on?

RMSD
  • 476
  • 5
  • 12
  • The only other thing I can think of is that the website happens to be https. I'm not sure the mechanize can handle the encryption. – RMSD Jun 28 '13 at 08:35

1 Answers1

0

Would like to add as a comment but am not able to: I doubt if the few hidden inputs especially the string serve as a verification to prevent CSRF attacks. Did you try submitting the hidden fields back along with email/password? That might help you out.

Yandong Liu
  • 335
  • 4
  • 13
  • Hmmm... I hadn't thought of that. I'll try it and see what happens. – RMSD Jun 26 '13 at 03:49
  • So I've tried to change them and they're all apparently read only except for the last one. The last one seems to change leading me to believe it's some sort of token auth? I'm not sure how to reference it though. – RMSD Jun 26 '13 at 22:27
  • Did you try adding User-Agent to the header? Like this: 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/13.0.1')] – Yandong Liu Jun 27 '13 at 04:20
  • Yeah, I had tried `br.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'), ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'), ('Accept-Encoding', 'gzip,deflate,sdch'), ('Accept-Language', 'en-US,en;q=0.8'), ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]`. I also tried the one you provided. Neither seemed to work. – RMSD Jun 27 '13 at 17:00