Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot sign in using form with Mechanize

I am trying to submit a form with mechanize. But nothing happens on submit. I just get the login page again.

The form:

http://affilate.mikkelsenmedia.dk/partnersystem/mylogins.php

require 'Mechanize'
agent = WWW::Mechanize.new
agent.get("http://affilate.mikkelsenmedia.dk/partnersystem/mylogins.php")

form = agent.page.forms.first
form.username = 'username'
form.password = 'password'
form.submit

Live http log when login in with browser:

http://affilate.mikkelsenmedia.dk/partnersystem/mylogins.php

POST /partnersystem/mylogins.php HTTP/1.1
Host: affilate.mikkelsenmedia.dk
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; da; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: da,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://affilate.mikkelsenmedia.dk/partnersystem/mylogins.php
Cookie: XXX
Content-Type: application/x-www-form-urlencoded
Content-Length: 47
username=username&password=password&send=Submit
HTTP/1.1 200 OK
Date: Fri, 18 Feb 2011 17:07:15 GMT
Server: Apache/2.0.63 (CentOS)
X-Powered-By: PHP/5.1.6
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 77
Connection: close
Content-Type: text/html; charset=UTF-8

Console:

irb(main):001:0> require 'Mechanize'
=> true
irb(main):002:0> agent = Mechanize.new { |agent|
irb(main):003:1*   agent.user_agent_alias = 'Mac Safari'
<"http://affilate.mikkelsenmedia.dk/partnersystem/mylogins.php")
=> #<Mechanize::Page
 {url
  #<URI::HTTP:0x2a1c770 URL:http://affilate.mikkelsenmedia.dk/partnersystem/mylo
gins.php>}
 {meta}
 {title "Partner System - Log-in"}
 {iframes}
 {frames}
 {links}
 {forms
  #<Mechanize::Form
   {name "loginform"}
   {method "POST"}
   {action "mylogins.php"}
   {fields
    #<Mechanize::Form::Text:0x2836680
     @name="username",
     @node=
      #(Element:0x141b3e8 {
        name = "input",
        attributes = [
          #(Attr:0x13e4458 { name = "type", value = "text" }),
          #(Attr:0x13e4440 { name = "name", value = "username" }),
          #(Attr:0x13e43e0 { name = "size", value = "30" })]
        }),
     @value="">
    #<Mechanize::Form::Field:0x2836230
     @name="password",
     @node=
      #(Element:0x141b22c {
        name = "input",
        attributes = [
          #(Attr:0x13ac148 { name = "type", value = "password" }),
          #(Attr:0x13ac13c { name = "name", value = "password" }),
          #(Attr:0x13ac10c { name = "size", value = "30" })]
        }),
     @value="">}
   {radiobuttons}
   {checkboxes}
   {file_uploads}
   {buttons
    #<Mechanize::Form::Submit:0x2835f90
     @name="send",
     @node=
      #(Element:0x141b01c {
        name = "input",
        attributes = [
          #(Attr:0x13702e0 { name = "type", value = "submit" }),
          #(Attr:0x13702d4 { name = "name", value = "send" }),
          #(Attr:0x13702c8 { name = "class", value = "style2" }),
          #(Attr:0x13702bc { name = "value", value = "Submit" })]
        }),
     @value="Submit">}>}>

irb(main):006:0>   form = agent.page.forms.first
irb(main):007:0>   form.username = 'username'
=> "username"
irb(main):008:0>   form.password = 'password'
=> "password"
irb(main):009:0>   form.submit
=> #<Mechanize::Page
 {url
  #<URI::HTTP:0x2a82e78 URL:http://affilate.mikkelsenmedia.dk/partnersystem/mylo
gins.php>}
 {meta}
 {title "Partner System - Log-in"}
 {iframes}
 {frames}
 {links}
 {forms
  #<Mechanize::Form
   {name "loginform"}
   {method "POST"}
   {action "mylogins.php"}
   {fields
    #<Mechanize::Form::Text:0x2a52c50
     @name="username",
     @node=
      #(Element:0x1529694 {
        name = "input",
        attributes = [
          #(Attr:0x1513c14 { name = "type", value = "text" }),
          #(Attr:0x1513c08 { name = "name", value = "username" }),
          #(Attr:0x1513bfc { name = "size", value = "30" })]
        }),
     @value="">
    #<Mechanize::Form::Field:0x2a52998
     @name="password",
     @node=
      #(Element:0x1529550 {
        name = "input",
        attributes = [
          #(Attr:0x15121d4 { name = "type", value = "password" }),
          #(Attr:0x15121c8 { name = "name", value = "password" }),
          #(Attr:0x15121bc { name = "size", value = "30" })]
        }),
     @value="">}
   {radiobuttons}
   {checkboxes}
   {file_uploads}
   {buttons
    #<Mechanize::Form::Submit:0x2a52758
     @name="send",
     @node=
      #(Element:0x152940c {
        name = "input",
        attributes = [
          #(Attr:0x151062c { name = "type", value = "submit" }),
          #(Attr:0x1510614 { name = "name", value = "send" }),
          #(Attr:0x1510608 { name = "class", value = "style2" }),
          #(Attr:0x15105fc { name = "value", value = "Submit" })]
        }),
     @value="Submit">}>}>

irb(main):010:0>
like image 381
Rails beginner Avatar asked Dec 13 '22 15:12

Rails beginner


2 Answers

The server also checks the presence of the submit button parameter 'send'.

Add this line before the form.submit

form.add_field! 'send','Submit'
like image 147
Michiel de Mare Avatar answered Dec 28 '22 06:12

Michiel de Mare


One thing I saw running your example:

>> agent = WWW::Mechanize.new
!!!!! DEPRECATION NOTICE !!!!!
The WWW constant is deprecated, please switch to the new top-level Mechanize
constant.  WWW will be removed in Mechanize version 2.0

You've referenced the WWW constant from (irb):3:in `irb_binding', please
switch the "WWW" to "Mechanize".  Thanks!

Sincerely,

  Pew Pew Pew

I don't have an account on that system so logging in won't work for me. What do you see if you run your initial steps in IRB?

like image 41
the Tin Man Avatar answered Dec 28 '22 04:12

the Tin Man