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

    これは私がこのphantomjs not waiting for "full" page loadを読みましたが、私の問題は解決しませんでした。私のjsにPhantomJSとCasperJSがロードされていません

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

    そして、この:

    私はこの(全体ではない私のHTML)を試した私もスクリーンショットを取っていた

    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

    それは少し難しいです準備ができているツールを試してみることをおすすめしますか? (免責事項:私はその小さな図書館を作った)。待ち受けがあります組み込み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(); 
    
    関連する問題