我正在尝试使用Ruby和Mechanize登录网站,但是使用一种特定的登录表单我遇到了麻烦。出于某种原因,Mechanize似乎不承认这种形式。
Mechanize.new.get("website").form
返回nil,forms
返回[]
。
agent = Mechanize.new
fm_raw_mechanize = agent.get("https://www.fitmob.com/login")
fm_form = fm_raw_mechanize.form #nil
fm_form.login = "rebeccamhathaway@gmail.com"
fm.password = "classpass"
fm.submit
这里有什么想法吗?
我应该尝试其他类似的宝石(推荐)吗?
这是"表格"的源代码。请注意JavaScript on_click
:
<div>
<div class="email_reg" id="email_reg">
<div id="email_box" class="form-group ">
<label for="email"></label>
<input type="text" name="email" id="email" value="kevin@gmail.com" placeholder="Email address">
</div>
<div id="pwd_box" class="form-group error">
<label for="pwd"></label>
<input type="password" name="pwd" id="pwd" placeholder="Password">
<span id="pwd_info" class="err">Valid passwords have 6 characters or more</span>
</div>
<a href="#" onclick="javascript:loggingTrack('web: pw reset click from login', {}, function(resp) {document.location = '/password/reset';});" class="forgot_pw">Forgot your password?</a>
</div>
<button id="login_email">Log in via email</button>
<span class="divider">or</span>
<div id="login_fb_box">
<button id="login_fb">Facebook Connect</button>
</div>
<span class="alternative">Don't have an account? <a href="/signup">Request an invite</a></span>
</div>
答案 0 :(得分:0)
您是否尝试过以下内容:
$ pry
require 'mechanize'
agent = Mechanize.new
fm_raw_mechanize = agent.get('https://www.fitmob.com/login')
email_field = fm_raw_mechanize.at "#email"
email_field.attributes["value"].value = "me@email.com"
fm_raw_mechanize.at('input[@name="pwd"]')['value'] = "password"
agent.click(fm_raw_mechanize.at("#login_email"))