Ruby 使用带有Mechanize的登录表单

Ruby 使用带有Mechanize的登录表单,ruby,mechanize,mechanize-ruby,Ruby,Mechanize,Mechanize Ruby,我知道在Stackoverflow上也有类似的帖子,但我仍然不知道我的尝试有什么问题 # 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 = use

我知道在Stackoverflow上也有类似的帖子,但我仍然不知道我的尝试有什么问题

# 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
这是我的错误:

rb:18:in `block (2 levels) in <main>': undefined method `field_with' for nil:NilClass (NoMethodError)
rb:18:in`block(2层)in':nil:NilClass(NoMethodError)的未定义方法'field_with'
这是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>

带*的字段是必需的

电子邮件地址* 密码* 还记得我在这台电脑上吗

第页

#<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}>
试试这个:

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
respond = mech.post('www.mysite.com/site/login', "LoginForm[username]" => userName,    "LoginForm[password]" => password)

我在
:action=>“/site/login/”
的末尾删除了
/
,并将
提交链接到块的后面,如中所示。但是我没有运行代码,所以请告诉我它是否有效。

您让它变得比需要的更复杂:

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

你为什么不直接发送post请求?试试这个:

respond = mech.post('www.mysite.com/site/login', "LoginForm[username]" => userName,    "LoginForm[password]" => password)
如果您知道目标(/site/login)、请求方法(post)和必须发送的参数,请在不影响表单的情况下执行post请求


让我知道它是否有效。

Ruby告诉您
f
nil
,因此可能没有找到表单。我们可以看一下表单的HTML吗?我想你应该写
f.submit
而不是
表单。submit
@padde更改了f.submit请检查
页面
,在
l=page.form\u行前面插入
p页
,然后在这里发布输出。好了,如果您需要更多信息,请告诉我。最后一行(
{forms}
)告诉您页面上没有表单。尝试
curl
以检查您是否从Web服务器获得预期的响应(即包含表单)。好的,我真的不明白这一点,当我在IRB中执行此操作时,我看到一个difernet输出,它是否在重定向之后