Unable to enter form using Mechanize

I am trying to submit a form using mechanization. But nothing happens when serving. I 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 entering the 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> 
+4
source share
3 answers

The server also checks for the send button option to send.

Add this line before form.submit

 form.add_field! 'send','Submit' 
+11
source

One thing I saw in 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 do not have an account on this system, so logging in will not work for me. What do you see if you are starting your first steps in IRB?

+2
source

When you run "agent = WWW :: Mechanize.new", you will get a DEPRECATION warning, because the constant "WWW" is deprecated in the latest release. You need to run the command without a permanent www. The modified command will be:

agent = Mechanize.new

thanks

0
source

Source: https://habr.com/ru/post/1340353/


All Articles