javascript - PhantomJS includeJs() + 嵌套评估() 不工作

标签 javascript jquery phantomjs

考虑以下代码...

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();
});

我正在尝试获取一些插入 jquery 的代码,然后允许我继续执行操作,但它不会评估 includeJs()

最佳答案

编辑: 正如 Vaviloff 在下面的评论中提到的,您需要订阅“page.onConsoleMessage”事件才能在 page.evaluate() 回调中使用 console.log() 。我更新了下面的代码块以反射(reflect)这一点。

以下代码将使用 jQuery 从页面捕获用户代理文本,并捕获页面内容已被操纵的证据。

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);
  }
});

控制台输出结果:

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

关于javascript - PhantomJS includeJs() + 嵌套评估() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732381/

相关文章:

javascript - Node.js - 为什么在使用 mocha 和 zombie 进行测试时会出现泄漏?

javascript - 删除 mouseleave 上的 contenteditable 属性

javascript - 为什么背景颜色闪烁而不是平滑变化?

javascript - 我想要有可以搜索我的图像的链接

javascript - CasperJS:评估()返回空值

javascript - 有没有办法对网站上的每个页面进行截图?

coffeescript - 如何使用phantomjs从站点下载图像

javascript - Node.js 将 : store req. ip 表达为变量

javascript - 为什么当我下载我的数组并尝试获取它的长度时,它只是吐出数组的字节数

javascript - 时区 API INVALID_REQUEST