简体   繁体   English

输入不使用Mechanize / Ruby表单的网站的登录信息

[英]Entering login information for a site that doesn't use forms with Mechanize/Ruby

I need to log in to a site to run some scripts. 我需要登录到站点以运行一些脚本。 Mechanize with Nokogiri looks perfect for the task, but I'm having a hard time inputting the login information since the input fields aren't located in forms (agent.page.forms.first yields nil): 使用Nokogiri进行机械化看起来非常适合该任务,但是由于输入字段不在表单中(agent.page.forms.first产生nil),因此我很难输入登录信息:

    <div class="loginform" id="login_form">
      <div>
        <input type="text" id="loginname" tabindex="1"/>
      </div>
      <div>
        <input type="password" id="password" tabindex="2"/>
        <input type="text" id="password_text" style="display:none;"/>
      </div>
      <div class="signin"><a href="javascript:void(0);" class="sp" id="login_submit_btn"></a>
      </div>
    </div>

Is it possible to somehow navigate to the input elements (they're the only ones on the page) and then submit my username and password to log in? 是否可以通过某种方式导航到输入元素(它们是页面上的唯一元素),然后提交我的用户名和密码进行登录? If Mechanize isn't capable of doing something like this, are there alternatives? 如果Mechanize无法执行此类操作,是否有其他选择? Thanks so much. 非常感谢。

I think you would better check inside the whole source (including javascript) how the website connect you. 我认为您最好检查一下整个来源(包括javascript),该网站如何与您建立联系。 It should be a HTTP POST request in my opinion... and use it to log directly without parsing the main page by GET request but using Mechanize post method instead : 我认为这应该是HTTP POST请求...,并使用它直接登录,而无需通过GET请求解析主页,而是使用Mechanize post method

agent = Mechanize.new
url = 'http://website.com/hidden_login_page.php'
page = agent.post(url, {"login" => "foo", "password" => "bar"})

Hope it helps too. 希望它也有帮助。

I recently had an issue like this. 我最近遇到这样的问题。 Try the following in mechanize: 在机械化中尝试以下操作:

page.field_with(:name => "loginname").value = "foo"
page.field_with(:name => "password").value = "bar"

Hope this helps. 希望这可以帮助。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM