javascript
  • phantomjs
  • casperjs
  • 2017-06-19 93 views 0 likes 
    0

    我讀過這phantomjs not waiting for "full" page load但它並沒有解決我的問題。PhantomJS和CasperJS不加載JS

    我已經試過這(我的HTML,而不是整個):

    <div id="test"></div> 
    
    
    <script> 
        window.onload = function() { 
         document.getElementById('test').innerHTML = 'something'; 
         console.log('page loaded'); 
        }; 
    </script> 
    

    而這在我的JS:

    var page = require('webpage').create(); 
    
    page.onConsoleMessage = function(msg) { 
        console.log('here i am'); 
    }; 
    page.onLoadFinished = function() { 
        console.log('or here!'); 
    } 
    page.open('http://localhost:3456/calculatorfixture.html', function (status) { 
        waitFor(function _test(){ 
         return page.evaluate(function() { 
          return document.getElementById("test").innerHTML == "something"; 
         }); 
        }, function _onReady(){ 
         console.log ("DONE"); 
         phantom.exit(); 
        }); 
    }); 
    

    我正在截圖也 - 依然,沒有什麼。我無法使用任何JS在我的頁面上工作。我錯過了什麼?

    回答

    0

    問題是你錯過了JS中的waitFor()函數。我不認爲它是內置於PhantomJS - 它在waitfor.js示例代碼中列出。有一次,我補充說,JS文件,只是require聲明後,我得到了以下的輸出:

    here i am 
    or here! 
    'waitFor()' finished in 500ms. 
    DONE 
    

    WAITFOR()代碼:

    /** 
    * Wait until the test condition is true or a timeout occurs. Useful for waiting 
    * on a server response or for a ui change (fadeIn, etc.) to occur. 
    * 
    * @param testFx javascript condition that evaluates to a boolean, 
    * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 
    * as a callback function. 
    * @param onReady what to do when testFx condition is fulfilled, 
    * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 
    * as a callback function. 
    * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used. 
    */ 
    function waitFor(testFx, onReady, timeOutMillis) { 
        var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s 
         start = new Date().getTime(), 
         condition = false, 
         interval = setInterval(function() { 
          if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) { 
           // If not time-out yet and condition not yet fulfilled 
           condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code 
          } else { 
           if(!condition) { 
            // If condition still not fulfilled (timeout but condition is 'false') 
            console.log("'waitFor()' timeout"); 
            phantom.exit(1); 
           } else { 
            // Condition fulfilled (timeout and/or condition is 'true') 
            console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms."); 
            typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled 
            clearInterval(interval); //< Stop this interval 
           } 
          } 
         }, 250); //< repeat check every 250ms 
    }; 
    
    +0

    我確實擁有它。正好在我放的js代碼之上。 – mmmm

    +0

    您應該更新您的問題以顯示完整的代碼以及預期的輸出。如果我的本地測試給出了正確的結果,我傾向於認爲您的配置中存在另一個問題。您是否在http:// localhost:3456/calculatorfixture.html驗證了內容,行爲和JS錯誤的缺失?嘗試將其剝離到要點並再次測試。 –

    0

    這是一個有點難以看到的問題沒有完全再現,我可以建議你嘗試一個現成的工具嗎? (免責聲明:我製作了這個小型圖書館)。有一個waitFor內置https://github.com/javascriptlove/haunt

    var page = require('./build/haunt.js'); 
    
    page.create({ 
         log: true, 
         userAgent: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36' 
        }) 
        .get('http://example.com') 
        .waitFor(function() { 
         return document.getElementById("test").innerHTML == "something"; 
        }) 
        .then(function() { 
         // other stuff you want to do, this.page refers to the phantom's page object 
        }) 
        .end(); 
    
    相關問題