簡體   English   中英

使用node.js中的僵屍填寫登錄表單

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

晚間! 我正在嘗試使用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")它顯然確實找到了表單,但是唉,由於某種原因,它不喜歡它。

我還嘗試了僵屍的“常規”方法,它使用網頁上的登錄按鈕並按下它。 問題是它實際上不是一個按鈕,只是一個附有鏈接的圖像,而且它都在<span> 我不知道如何“點擊”該按鈕。

它沒有ID,所以我不能使用它,然后我嘗試使用它上面的文本,但因為它上面有變音符號我無法使用它。 使用/ 344轉義ä只會出錯:

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

所以,是的,這不起作用,雖然我不知道為什么它不能正確識別逃逸的變形金剛。

這是我的第一個問題,第二個問題是次要問題,但我現在為什么不在這里問它,我已經寫了這篇文章。

如果我讓所有這些工作,我可以以某種方式復制此登錄給我的cookie,並在我的YQL中使用它來進行屏幕抓取嗎? 基本上我是在試圖榨取股票市場價值,但如果沒有登錄值,則延遲15分鍾,這不是太糟糕,但無論如何我都希望它能夠存在。

在使用僵屍進行了幾次測試之后,我得出的結論是,使用它進行嚴格的測試還為時尚早。 不過,我想出了表單提交的工作示例(使用常規.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方法)。 注意:歷史對象將包含您提交表單的頁面地址,即使您沒有等待頁面加載 - 它讓我感到困惑,因為我確信我應該已經看到了新頁面。


編輯 :對於您的網站,僵屍似乎工作正常(我可以提交表單並獲得“錯誤的登錄或密碼”警報)。 有一些JS錯誤,但僵屍不關心它們(你應該調試那些,看看腳本是否適用於普通用戶)。 無論如何,這是我使用的腳本:

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 - 即使我用字符串填充查詢框並提交表單我沒有得到搜索結果。 原因? 可能谷歌采取了一些措施來阻止自動工具(如僵屍)瀏覽搜索結果。
  • bing.com - 與google一樣 - 在提交表單后我沒有得到搜索結果。 原因? 可能與谷歌相同。
  • paulirish.com - 填寫搜索查詢框並提交表單僵屍后遇到腳本錯誤,阻止它完成頁面(關於從圖表腳本中丟失ActiveX的事情)。
  • perfectionkills.com - 令人驚訝的是,我遇到了與Paul Irish網站相同的問題 - 由於javascript錯誤,無法加載搜索結果頁面。

結論:畢竟迫使僵屍去做你的工作並不容易...... :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM