2017-02-12 58 views
1

我已經成功地獲取原始html(已與其他產品一起檢索),然後讓phantomjs採用原始html並渲染整個頁面,包括運行任何/所有javascript。我最近遇到了一個沒有呈現JavaScript的頁面。PhantomJs無法呈現源文件中的特定頁面

這是我如何運行它...

phantomjs myscript.js > OUTPUT.txt 2>&1 

下面是一個說明該問題的myscript.js文件...

var page = require('webpage').create(), 
var system = require('system'); 
var address = 'http://cloud.firebrandtech.com/#!/login'; 
var rawHtml = '<!DOCTYPE html>\ 
<html>\ 
<head>\ 
    <meta charset="utf-8">\ 
<meta http-equiv="X-UA-Compatible" content="IE=edge">\ 
<meta name="viewport" content="width=device-width, initial-scale=1.0">\ 
<meta name="description" content="Web Portal for managing Cloud Products, Assets, and Distributions">\ 
<meta name="author" content="Firebrand Technologies">\ 
<title>Firebrand Cloud</title>\ 
<link rel="stylesheet" href="/widgets/css/widgets.css">\ 
<link rel="stylesheet" href="/css/portal.css">\ 
</head>\ 
<body ng-app="portal" fc-app="cloud" fc-direct="true" class="fc">\ 
    <div>\ 
     <div data-ng-if="user.isLoaded" data-ng-controller="PortalCtrl">\ 
      <div data-ng-include="getView()"></div>\ 
      <div class="container">\ 
       <div data-ui-view></div>\ 
      </div>\ 
     </div>\ 
    </div>\ 
    <script src="/widgets/js/widgets.js"></script>\ 
<script src="/js/vendor.js"></script>\ 
<script src="/js/portal.js"></script>\ 
</body>\ 
</html>'; 

page.settings.resourceTimeout = 5000; 
page.settings.loadImages = false; 
page.setContent(rawHtml, address); 
window.setTimeout(function() { 
    if(page.content.indexOf('Sign In') > -1) 
     console.log('YAY!!! Javascript Rendered!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') 
    else 
     console.log('BOO!!! Javascript NOT Rendered!!!!!!!!!!!!!!!!!!!!!!!!!!')  

    phantom.exit(); 
}, 5000); 

好像這個頁面需要一些認證/ CORS上班。如果phantomjs使用實際的請求(使用page.open)來獲取源代碼,我可以讓它工作。但是,這個解決方案對我來說不起作用。 Phantomjs必須使用上述示例中的源代碼(正如我所提到的,它一直在爲所有其他站點工作)。

var page = require('webpage').create(), 
var system = require('system'); 
var address = 'http://cloud.firebrandtech.com/#!/login '; 

page.open(address, function(status) { 
    setTimeout(function(){ 
     if(page.content.indexOf('Sign In') > -1) 
      console.log('YAY!!! Javascript Rendered!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') 
     else 
      console.log('BOO!!! Javascript NOT Rendered!!!!!!!!!!!!!!!!!!!!!!!!!!')  

     phantom.exit(); 
    }, 5000) 
}); 

我已經使用標誌像下面已經嘗試過,但他們似乎沒有任何效果...

phantomjs --web-security=false --ignore-ssl-errors=true thefilebelow.js > OUTPUT.txt 2>&1 

回答

0

終於得到了這個工作......

,因爲我用其他產品(不phantomjs)來檢索頁面源,我需要堅持發送與該請求發回的cookie。然後我不得不通過這些餅乾使用addCookie像這樣...

var page = require('webpage').create(), 
var system = require('system'); 
var address = 'http://cloud.firebrandtech.com/#!/login'; 
var rawHtml = 'same raw html as above...'; 

//THE NEXT 3 LINES ARE WHAT CHANGED 
var cookiesFromInitialRequest = [{name: 'aaa', value: 'bbb', domain: 'ccc'}, etc...] 
for(var i = 0; i < cookiesFromInitialRequest.length; i++) 
    phantom.addCookie(cookiesFromInitialRequest[i]) 

page.settings.resourceTimeout = 5000; 
page.settings.loadImages = false; 
page.setContent(rawHtml, address); 
window.setTimeout(function() { 
    if(page.content.indexOf('Sign In') > -1) 
     console.log('YAY!!! Javascript Rendered!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') 
    else 
     console.log('BOO!!! Javascript NOT Rendered!!!!!!!!!!!!!!!!!!!!!!!!!!')  

    phantom.exit(); 
}, 5000); 
+0

所以......這是你的問題的答案? – Vaviloff

+0

是的,我只是不能選擇它作爲答案,直到明天。 – sjdirect