2014-11-24 26 views
0

我正在部署一個Zombiejs應用程序到Openshift,但殭屍似乎無法獲取HTML。Zombiejs空的HTML

我有一個對象(稱爲撲克),它維護無頭瀏覽器並用它做事情。其中一種稱爲init的方法登錄到網站並返回「初始化」瀏覽器。

Poker.prototype.init = function (email, password, ip) { 
    var self = this; 
    var browser; 

    // Have to use the ip that Openshift provides 
    // See SO question http://goo.gl/n2TfMC 
    if (ip) { 
    browser = Zombie.create({ 
     'localAddress': ip 
    }); 
    } else { 
    browser = Zombie.create(); 
    } 

    return new Promise(function (resolve, reject) { 
    browser 
     .visit('http://some.login/page') 
     .then(function() { 
     // Some debugging stuff :p 
     console.log('body: '); 
     console.log(browser.html('body')); 

     // Fill in the credentials 
     browser.fill('email', email); 
     browser.fill('password', password); 
     return browser.pressButton('Log In'); 
     }) 
     .done(function() { 
     // Logged in, new page loaded 
     // Check if login was successful 
     var title = browser.text('title'); 
     console.log(title); 
     }); 
    }); 
} 

控制檯打印body:後保持爲空,anden殭屍試圖填寫的郵箱地址,我收到此錯誤:

Possibly unhandled TypeError: Cannot use 'in' operator to search for 'compareDocumentPosition' in null 
    at /var/lib/openshift/[app-id]/app-root/runtime/repo/node_modules/zombie/node_modules/jsdom/node_modules/nwmatcher/src/nwmatcher-noqsa.js:267:43 
    at module.exports (/var/lib/openshift/[app-id]/app-root/runtime/repo/node_modules/zombie/node_modules/jsdom/node_modules/nwmatcher/src/nwmatcher-noqsa.js:37:7) 
    at addNwmatcher (/var/lib/openshift/[app-id]/app-root/runtime/repo/node_modules/zombie/node_modules/jsdom/lib/jsdom/selectors/index.js:6:27) 
    at HTMLDocument.<anonymous> (/var/lib/openshift/[app-id]/app-root/runtime/repo/node_modules/zombie/node_modules/jsdom/lib/jsdom/selectors/index.js:18:29) 
    at HTMLDocument.querySelectorAll (/var/lib/openshift/[app-id]/app-root/runtime/repo/node_modules/zombie/node_modules/jsdom/lib/jsdom/level1/core.js:63:53) 
    at Browser.queryAll (/var/lib/openshift/[app-id]/app-root/runtime/repo/node_modules/zombie/lib/zombie/browser.js:348:26) 
    at Browser.field (/var/lib/openshift/[app-id]/app-root/runtime/repo/node_modules/zombie/lib/zombie/browser.js:591:17) 
    at Browser._findOption (/var/lib/openshift/[app-id]/app-root/runtime/repo/node_modules/zombie/lib/zombie/browser.js:662:18) 
    at Browser.select (/var/lib/openshift/[app-id]/app-root/runtime/repo/node_modules/zombie/lib/zombie/browser.js:692:19) 
    at /var/lib/openshift/[app-id]/app-root/runtime/repo/poker.js:61:17 

我看到這個之後,我試圖visit不同的頁面(谷歌)通過殭屍,但它也返回空的HTML。

我看了一下關於compareDocumentPosition錯誤的其他一些StackOverflow問題,但我認爲我所使用的問題與Openshift的部署有關,而不是與我正在訪問的頁面的HTML有關。

我使用Node.js v0.10.25和Zombie v2.2.1。

+0

也許'.then'不會等待頁面完全加載。看到[這個答案](http://stackoverflow.com/questions/19101258/why-does-the-zombie-js-browser-return-empty-html-when-using-bootstrap)一個可能的解決方法 – yuvi 2014-11-26 15:56:06

+0

我試過使用'.wait()',主體仍然是空的 – apparatix 2014-11-26 16:08:19

+1

他不只是使用'wait',他運行一個函數來檢查特定DOM元素的存在,並等待它出現。例如'.wait(foo,function(){...});',其中'foo'返回'window.document.querySelector(「。my-dom-element」);' – yuvi 2014-11-26 16:10:45

回答

1

它可能是一個CORS問題? .login不是有效的TLD,每個TLD的規則也不盡相同。

+0

URL實際上並不是some.login/page,那是一個佔位符 – apparatix 2014-12-03 07:17:23

0

您確定您正在嘗試使用的變量「瀏覽器」是對象,如果該變量是字符串,則可能會發生該錯誤。

+0

如果它是這樣的話,請使用JSON.parse(variableName)將它從字符串轉換爲JSON對象。 – user2048239 2014-12-03 03:49:00

+0

在本地運行相同的確切代碼完美地工作;也Zombie.create確實返回一個瀏覽器對象。 – apparatix 2014-12-03 07:17:59