晚上好!我正在尝试使用 zombie.js 登录网站,但我似乎无法使其正常工作。 哦,该网站是芬兰语的,但并不难理解,有两个文本字段和一个按钮。第一个是用户名,第二个是密码,按钮是登录按钮。
目前我的登录代码如下:
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);
});
现在我知道我也许应该使用 zombie 自己的“填充”方法,但我尝试过但没有成功,所以我尝试了一些新方法。
我从这里得到的只是一个错误:
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
现在如果我记录 browser.document.getElementById("loginForm")
它显然确实找到了表格,但遗憾的是,出于某种原因它不喜欢它。
我还尝试了 zombie 的“常规”方法,即使用网页上的登录按钮并按下它。问题是它实际上不是一个按钮,只是一个附有链接的图像,而且都在 <span>
中。 .而且我不知道如何“点击”那个按钮。
它上面没有 ID,所以我不能使用它,然后我尝试使用上面的文本,但是因为上面有变音符号,所以我无法使用它。使用/344 转义 ä 只会出错:
throw new Error("No BUTTON '" + selector + "'");
^
Error: No BUTTON 'Kirjaudu sisään'
是的,那没有用,尽管我不知道为什么它不能正确识别转义的变音符号。
这是我的第一个问题,第二个是一个小问题,但既然我已经写完了这篇文章,我想为什么不在这里问呢。
如果我让这一切正常工作,我能否以某种方式复制此登录给我的 cookie,并在我的 YQL 中使用它进行屏幕抓取?基本上我正在尝试抓取股票市场值(value),但如果没有登录,这些值(value)会延迟 15 分钟,这还不错,但我希望它仍然存在。
最佳答案
在使用 zombie 进行了几次测试后,我得出的结论是,使用它进行正式测试还为时过早。不过,我提出了表单提交的工作示例(使用常规 .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
对象仍将引用初始页面(作为参数传递给 visit
方法的页面)。注意:历史对象将包含您提交表单的页面地址,即使您不等待页面加载 - 这让我有点困惑,因为我确信我应该已经看到新页面。
编辑: 对于您的网站, zombie 似乎工作正常(我可以提交表单并收到“错误的登录名或密码”警报)。有一些 JS 错误,但 zombie 并不关心它们(你应该调试这些错误以查看脚本是否对普通用户正常工作)。无论如何,这是我使用的脚本:
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 - 尽管我在查询框中填写了一个字符串并提交了表单,但我没有得到搜索结果。原因?可能谷歌采取了一些措施来防止自动工具(例如 zombie )浏览搜索结果。
- bing.com - 与 google 相同 - 提交表单后我没有得到搜索结果。原因?可能与谷歌相同。
- paulirish.com - 在填写搜索查询框并提交表格后, zombie 遇到了脚本错误,导致它无法完成页面(图表脚本缺少 ActiveX)。
- perfectionkills.com - 令人惊讶的是,我在这里遇到了与 Paul Irish 网站相同的问题 - 由于 javascript 错误,无法加载包含搜索结果的页面。
结论:毕竟强制 zombie 来做你的工作并不是那么容易...:)
关于javascript - 在 node.js 中用 zombie 填充登录表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12092877/