5

I know there are very similar posts to this on Stackoverflow but I still can't seem to figure out what is wrong with my attempt.

# login to the site 
mech.get(base_URL) do |page|
  l = page.form_with(:action => "/site/login/") do |f|
    username_field = f.field_with(:name => "LoginForm[username]")
    username_field.value = userName
    password_field = f.field_with(:name => "LoginForm[password]")
    password_field.value = password
    f.submit
  end
end

Here is my error:

rb:18:in `block (2 levels) in <main>': undefined method `field_with' for nil:NilClass (NoMethodError)

Here is the HTML

<div class="bucketbody">

    <div class="form padding10">
    <form id="login-form" action="/site/login" method="post">
        <div class="row">
            <p class="note float_right">Fields with <span class="required">*</span> are required.</p>
            <label for="LoginForm_username" class="required">Email address <span class="required">*</span></label>              <input class="width_66per" autofocus="" name="LoginForm[username]" id="LoginForm_username" type="text">                         </div>

        <div class="row">
            <label for="LoginForm_password" class="required">Password <span class="required">*</span></label>               <input class="width_66per" name="LoginForm[password]" id="LoginForm_password" type="password">                          </div>

                    <div class="row rememberMe nolabel">
            <span class="field"><input id="ytLoginForm_rememberMe" value="0" name="LoginForm[rememberMe]" type="hidden"><input name="LoginForm[rememberMe]" id="LoginForm_rememberMe" value="1" type="checkbox">                <label for="LoginForm_rememberMe">Remember me on this computer</label>                              </span>
        </div>

        <p class="note"><a href="http://test.XXXXXXXX.com/user/reset">Forgot your password?</a></p>

        <div class="row buttons" style="padding-left: 0px;">
            <input class="pushButton" name="yt0" value="Login" type="submit">           </div>

    </form>     </div><!-- form -->

</div>

p page

#<Mechanize::Page
 {url #<URI::HTTP:0x225ce70 URL:http://xxxxxx.com/>}
 {meta_refresh}
 {title "xxxxxxxxxxx | xxxxxxxxx"}
 {iframes}
 {frames}
 {links
  #<Mechanize::Page::Link "\r\n                        " "/">
  #<Mechanize::Page::Link "About xxxxxx" "/features">
  #<Mechanize::Page::Link "xxxxx Overview" "/features">
  #<Mechanize::Page::Link "xxxxxxx for Associations" "/associations">
  #<Mechanize::Page::Link "xxxxxx For Education" "/education">
  #<Mechanize::Page::Link "FAQ" "/faq">
  #<Mechanize::Page::Link "About Us" "/aboutus">
  #<Mechanize::Page::Link "About Us" "/aboutus">
  #<Mechanize::Page::Link "News & Events" "/news-events">
  #<Mechanize::Page::Link "Environmental Commitment" "/environment">
  #<Mechanize::Page::Link "Our Team" "/ourteam">
  #<Mechanize::Page::Link "The xxxxxxxxxx" "/xxxxxxxxxx">
  #<Mechanize::Page::Link "Free Trial" "/freetrial">
  #<Mechanize::Page::Link "Contact" "/contacts">
  #<Mechanize::Page::Link "Contact us" "/contacts">
  #<Mechanize::Page::Link
   "xxxxxxxxxx"
   "http://www.xxxxxxxx.com/services/web-services/">
  #<Mechanize::Page::Link
   "inquire@xxxxxxxxx.com"
   "mailto:inquire@xxxxxxxxxxx.com">
  #<Mechanize::Page::Link xxxxxx" "http://www.xxxxxxxxx.com/">
  #<Mechanize::Page::Link
   "Technology Association of Oregon"
   "http://www.techoregon.org/">
  #<Mechanize::Page::Link "" "http://www.terrapass.com/">
  #<Mechanize::Page::Link "" "http://www.arborday.org/">}
 {forms}>
Zach
  • 885
  • 2
  • 8
  • 27

3 Answers3

5

You're making it more complicated than it needs to be:

page = mech.get base_URL

form = page.form # page.forms[1], etc.
form['LoginForm[username]'] = userName
form['LoginForm[password]'] = password

l = form.submit form.button
pguardiario
  • 53,827
  • 19
  • 119
  • 159
1

Why don't you send the post request directly? Try this:

respond = mech.post('www.mysite.com/site/login', "LoginForm[username]" => userName,    "LoginForm[password]" => password)

If you know the target (/site/login), the request method (post), and the params you must send, do the post request without messing with the form.

Let me know if it worked.

Alfonso
  • 758
  • 5
  • 8
0

Try this:

mech.get(base_URL) do |page|
  l = page.form_with(:action => "/site/login") do |f|
    username_field = f.field_with(:name => "LoginForm[username]")
    username_field.value = userName
    password_field = f.field_with(:name => "LoginForm[password]")
    password_field.value = password
  end.submit
end

i dropped the / at the end of :action => "/site/login/" and chained submit directly after the block, as in the mechanize example page. I did not run the code though, so let me know if it works.

Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
  • Please inspect `page` by inserting `p page` before the line `l = page.form_with #...` and post the output here. – Patrick Oscity Feb 18 '13 at 21:55
  • Ok there it is, let me know if you need more info – Zach Feb 18 '13 at 22:07
  • Well the last line (`{forms}`) tells you that there are no forms on the page. Try `curl` in order to check if you get the response from the webserver that you are expecting (i.e. containing the form). – Patrick Oscity Feb 18 '13 at 22:15
  • Ok I really don't understand this, When I do this in IRB I see a difernet output – Zach Feb 18 '13 at 22:15
  • could It not be following a redirect – Zach Feb 18 '13 at 22:16
  • Does it have a form? Something like `{forms # – Patrick Oscity Feb 18 '13 at 22:16
  • Very strange. Are you running this inside an app that uses bundler or so? Just checking that you use the same gem version in your script and IRB... If in doubt, `puts Mechanize::VERSION` in both script and IRB – Patrick Oscity Feb 18 '13 at 22:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24728/discussion-between-padde-and-zach) – Patrick Oscity Feb 18 '13 at 22:18
  • It looks like it is following a redirect in IRB get('http://xxxx.com') goes to URL:http://xxxx.com/site/login – Zach Feb 18 '13 at 22:18