简体   繁体   English

使用node.js中的僵尸填写登录表单

[英]Filling log in form with zombie in node.js

Evening! 晚间! I'm trying to log in into a website with zombie.js, but I don't seem to be able to make it work. 我正在尝试使用zombie.js登录到一个网站,但我似乎无法使其工作。 Oh and the website is in Finnish, but it's not very hard to understand, two text fields and a button. 哦,网站是芬兰语,但它不是很难理解,有两个文本字段和一个按钮。 First is for username, second for password and the button is the log in button. 第一个是用户名,第二个是密码,按钮是登录按钮。

At the moment my log in code is as follows: 目前我的登录代码如下:

var Browser = require("zombie");
browser = new Browser();
browser.visit("https://www.nordnet.fi/mux/login/startFI.html?cmpi=start-loggain",
    function () {
        // Here I check the title of the page I'm on.
        console.log(browser.text("title"));
        // Here I fill the needed information.
        browser.document.getElementById("input1").value ="MYUSERNAME";
        browser.document.getElementById("pContent").value ="MYPASSWORD";
        // And here it fails. I try to submit the form in question.
        browser.document.getElementById("loginForm").submit();
        setTimeout(function () {
            // This is here to check that we've submitted the info and have been
            // redirected to a new website.
            console.log(browser.text("title"));
        }, 2000);
});

Now I know that I maybe should have used zombie's own "fill" method, but I tried that with no luck so I tried something new. 现在我知道我可能应该使用zombie自己的“填充”方法,但我尝试了没有运气,所以我尝试了一些新的东西。

All I get from this is an error: 我从中得到的只是一个错误:

Y:\IMC\Development\Web\node_modules\zombie\lib\zombie\forms.js:72
  return history._submit(_this.getAttribute("action"), _this.getAttribute(
                 ^
TypeError: Cannot call method '_submit' of undefined

Now if I log that browser.document.getElementById("loginForm") it clearly does find the form, but alas, it doesn't like it for some reason. 现在,如果我记录了browser.document.getElementById("loginForm")它显然确实找到了表单,但是唉,由于某种原因,它不喜欢它。

I also tried the "conventional" method with zombie, which is using that log in button on the web page and pressing it. 我还尝试了僵尸的“常规”方法,它使用网页上的登录按钮并按下它。 The problem is that it's not actually a button, just an image which has a link attached to it, and it's all inside <span> . 问题是它实际上不是一个按钮,只是一个附有链接的图像,而且它都在<span> And I have no idea how I can "click" that button. 我不知道如何“点击”该按钮。

It has no ID on it, so I can't use that, then I tried to use the text on it, but because it has umlauts on it I can't get it to work. 它没有ID,所以我不能使用它,然后我尝试使用它上面的文本,但因为它上面有变音符号我无法使用它。 Escaping the ä with /344 only gave an error: 使用/ 344转义ä只会出错:

throw new Error("No BUTTON '" + selector + "'");
        ^
Error: No BUTTON 'Kirjaudu sisään'

So yeah, that didn't work, though I have no idea why it doesn't recognize the escaped umlaut correctly. 所以,是的,这不起作用,虽然我不知道为什么它不能正确识别逃逸的变形金刚。

This is my first question, the second one is a minor one, but I though why not ask it here too now that I've written this text. 这是我的第一个问题,第二个问题是次要问题,但我现在为什么不在这里问它,我已经写了这篇文章。

If I get all this to work, can I somehow copy the cookie that this log in gives me, and use that in my YQL for screen scraping? 如果我让所有这些工作,我可以以某种方式复制此登录给我的cookie,并在我的YQL中使用它来进行屏幕抓取吗? Basically I'm trying to scrape stock market values, but without the log in the values are 15min deferred, which isn't too bad, but I'd like it to be live anyhow. 基本上我是在试图榨取股票市场价值,但如果没有登录值,则延迟15分钟,这不是太糟糕,但无论如何我都希望它能够存在。

After couple of tests using zombie I came to the conclusion that it's still to early to use it for serious testing. 在使用僵尸进行了几次测试之后,我得出的结论是,使用它进行严格的测试还为时尚早。 Nevertheless, I came up with working example of form submit (using regular .submit() method). 不过,我想出了表单提交的工作示例(使用常规.submit()方法)。

var Browser = require("zombie");
var assert = require("assert");

browser = new Browser()
browser.visit("http://duckduckgo.com/", function () {
    // fill search query field with value "zombie"
    browser.fill('input[name=q]', 'mouse');
    // **how** you find a form element is irrelevant - you can use id, selector, anything you want
    // in this case it was easiest to just use built in forms collection - fire submit on element found
    browser.document.forms[0].submit();
    // wait for new page to be loaded then fire callback function
    browser.wait().then(function() {
        // just dump some debug data to see if we're on the right page
        console.log(browser.dump());
    })
});

As you can see, the clue is to use construct browser.wait().then(...) after submitting the form, otherwise browser object will still refer to the initial page (the one passed as an argument to visit method). 正如您所看到的,线索是在提交表单后使用构造browser.wait().then(...) ,否则browser对象仍将引用初始页面(作为参数传递给visit方法)。 Note: history object will contain address of page you submitted your form to even if you don't wait for the page to load - it confused me for a bit, as I was sure that I should already see the new page. 注意:历史对象将包含您提交表单的页面地址,即使您没有等待页面加载 - 它让我感到困惑,因为我确信我应该已经看到了新页面。


Edit : For your site, the zombie seems to be working ok (I could submit the form and get "wrong login or password" alert). 编辑 :对于您的网站,僵尸似乎工作正常(我可以提交表单并获得“错误的登录或密码”警报)。 There are some JS errors but zombie isn't concerned with them (you should debug those however to see if the script are working ok for regular users). 有一些JS错误,但僵尸不关心它们(你应该调试那些,看看脚本是否适用于普通用户)。 Anyhow, here's the script I used: 无论如何,这是我使用的脚本:

var Browser = require("zombie");
var assert = require("assert");

browser = new Browser()
browser.visit("https://www.nordnet.fi/mux/login/startFI.html?cmpi=start-loggain", function () {
    // fill in login field
    browser.fill('#input1', 'zombie');
    // fill in password field
    browser.fill('#pContent', 'commingyourway');
    // submit the form
    browser.document.forms[0].submit();
    // wait for new page to be loaded then fire callback function
    browser.wait().then(function() {
        console.log('Form submitted ok!');
        // the resulting page will be displayed in your default browser
        browser.viewInBrowser();
    })
});

As side note: while I was trying to come up with working example I've tried to user following pages (all have failed for different reasons): 作为旁注:当我试图提出工作示例时,我试图使用以下页面(所有都因为不同的原因而失败):

  • google.com - even though I filled query box with a string and submitted the form I didn't get search results . google.com - 即使我用字符串填充查询框并提交表单我没有得到搜索结果。 Reason? 原因? Probably google took some measures to prevent automatic tools (such as zombie) to browse through search results. 可能谷歌采取了一些措施来阻止自动工具(如僵尸)浏览搜索结果。
  • bing.com - same as google - after submitting the form I didn't get search results. bing.com - 与google一样 - 在提交表单后我没有得到搜索结果。 Reason? 原因? Probably same as for google. 可能与谷歌相同。
  • paulirish.com - After filling in the search query box and submitting the form zombie encountered script errors that prevent it from completing the page (something about missing ActiveX from charts script). paulirish.com - 填写搜索查询框并提交表单僵尸后遇到脚本错误,阻止它完成页面(关于从图表脚本中丢失ActiveX的事情)。
  • perfectionkills.com - Surprisingly here I've encountered the same problems as with Paul Irish site - page with search results couldn't be loaded due to javascript errors. perfectionkills.com - 令人惊讶的是,我遇到了与Paul Irish网站相同的问题 - 由于javascript错误,无法加载搜索结果页面。

Conclusion: It's not so easy to force zombie into doing your work after all... :) 结论:毕竟迫使僵尸去做你的工作并不容易...... :)

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

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