用node.js中的僵尸填写日志

晚间! 我试图用zombie.jslogin到一个网站,但我似乎无法使其工作。 哦,这个网站是芬兰文的,但是不难理解,两个文本框和一个button。 首先是用户名,第二次是密码,button是loginbutton。

目前我login代码如下:

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); }); 

现在我知道我也许应该使用僵尸自己的“填充”方法,但是我尝试了没有运气,所以我尝试了新的东西。

我从这里得到的是一个错误:

 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 

现在,如果我loginbrowser.document.getElementById("loginForm")它显然确实find窗体,但唉,它不喜欢它出于某种原因。

我也尝试了僵尸的“传统”方法,它使用网页上的loginbutton并按下它。 问题在于它实际上并不是一个button,只是一个链接到它的图像,它都在<span>里面。 我不知道如何“点击”该button。

它没有ID,所以我不能使用它,然后我试图使用它的文本,但因为它有变音,我不能得到它的工作。 用/ 344转义ä只出了一个错误:

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

所以是的,这是行不通的,虽然我不知道为什么它不能正确识别逃脱的变音符号。

这是我的第一个问题,第二个问题是小问题,但是我现在为什么不写这个问题呢。

如果我得到这一切的工作,我可以以某种方式复制这个日志的cookie给我,并使用我的YQL屏幕抓取? 基本上我试图刮去股市的价值,但没有login的价值是15分钟延期,这不是太糟糕,但我希望它是生活无论如何。

经过几次使用僵尸的testing之后,我得出的结论是,使用它进行严肃的testing还为时尚早。 不过,我想出了表单提交的工作示例(使用常规.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()); }) }); 

正如你所看到的,线索是在提交表单之后使用构造browser.wait().then(...) ,否则browser对象仍然会引用初始页面(作为parameter passing给visit方法的页面)。 注意:即使您不等待页面加载,历史logging对象也会包含您提交表单的页面地址 – 这让我困惑了一下,因为我确信我应该已经看到了新页面。


编辑 :为您的网站,僵尸似乎工作正常(我可以提交表单,并获得“错误的login名或密码”警报)。 有一些JS错误,但僵尸不关心他们(你应该debugging,但是看看脚本是否正常工作正常的用户)。 无论如何,这是我使用的脚本:

 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(); }) }); 

值得注意的是,当我试图拿出工作示例时,我尝试了用户关注的页面(由于不同的原因都失败了):

  • google.com – 即使我用string填充查询框,并提交表单,我没有得到search结果。 原因? 可能谷歌采取了一些措施,以防止自动工具(如僵尸)浏览search结果。
  • bing.com – 与谷歌相同 – 提交表单后,我没有得到search结果。 原因? 可能与谷歌相同。
  • paulirish.com – 填写search查询框并提交表单僵尸遇到脚本错误,阻止它完成页面(关于图表脚本丢失的ActiveX的东西)。
  • perfectionkills.com – 令人惊讶的是,在这里,我遇到了与Paul Irish网站相同的问题 – 页面search结果由于javascript错误而无法加载。

结论:强迫僵尸做你的工作并不是那么容易… 🙂