2017-01-19 3 views
2

Betrachten Sie den folgenden Code ...PhantomJS includeJs() + Nested evaluate() funktioniert nicht

var page = require('webpage').create(); 
console.log('The default user agent is ' + page.settings.userAgent); 
page.settings.userAgent = 'Lisas headless browser'; 
page.open('http://www.httpuseragent.org', function(status) { 
    if (status !== 'success') 
    { 
     console.log('Unable to access network or site is down'); 
    }else{ 
     page.includeJs(
      // Include the https version, you can change this to http if you like. 
      'https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js', 
      function() { 
       (page.evaluate(function() { 
        // jQuery is loaded, now manipulate the DOM 
        console.log(document.getElementById('myagent').textContent); 
       })) 
      } 
     ); 
    } 
    phantom.exit(); 
}); 

Ich versuche, einige Code zu bekommen gehen die jquery Einsätze und dann erlaubt mir Aktionen, die es durchführen, um fortzufahren, aber nicht auswertet includeJs()

Antwort

2

EDIT: Wie Vaviloff im Kommentar unten erwähnt, müssen Sie ‚page.onConsoleMessage‘ Ereignisse zu verwenden, um console.log() innerhalb des page.evaluate abonnieren () Rückrufen. Ich habe den folgenden Code-Block aktualisiert, um dies zu berücksichtigen.

Der folgende Code erfasst den Text des Benutzeragenten von der Seite mithilfe von jQuery und erfasst außerdem den Inhalt der Seite, der manipuliert wurde.

var page = require('webpage').create(); 
console.log('The default user agent is ' + page.settings.userAgent); 
page.settings.userAgent = 'Lisas headless browser'; 
// simple 'page.onConsoleMessage' event handler 
page.onConsoleMessage = function (msg) { 
    console.log('page.onConsoleMessage'); 
    console.log(msg); 
}; 
page.open('http://www.httpuseragent.org', function(status) { 
    if (status === 'success') { 
    // capture the rendered page to 'before.jpg' 
    page.render('before.jpg'); 
    // load the jQuery library 
    page.includeJs(
     'http://code.jquery.com/jquery-1.8.2.min.js', 
     function() { 
     // jQuery is loaded, now manipulate the DOM 
     var agent = page.evaluate(function() { 
      // This code is executed within the loaded page 
      // get the user agent string 
      var text = $('#myagent').text(); 
      // log the text to the console 
      console.log('Inside page.evaluate ::', text); 
      // time for some mischief 
      $('#myagent').html('PhantomJS Strikes Again!'); 
      // return the text string 
      return text; 
     }); 
     // capture the evidence 
     page.render('after.jpg'); 
     // print the user agent text 
     console.log(agent); 
     // exit now 
     phantom.exit(0); 
     }); 
    } else { 
    console.log('Unable to access network or site is down'); 
    phantom.exit(1); 
    } 
}); 

Resultierende Konsolenausgabe:

The default user agent is Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1 
page.onConsoleMessage 
Inside page.evaluate :: Your Http User Agent string is: Lisas headless browser 
Your Http User Agent string is: Lisas headless browser 
+3

Nun, eigentlich kann er innerhalb von page.evaluate verwenden console.log. Abonnieren Sie einfach Nachrichten von der Konsole mit [page.onConsoleMessage] (http://phantomjs.org/api/webpage/handler/on-console-message.html). – Vaviloff

+0

Danke. Ich habe etwas Neues gelernt - ich werde die Antwort aktualisieren. –