1

我的页面中有一个脚本标签,但它没有运行。它被简单地渲染。

我有以下代码:

var phantom = require('phantom');
phantom.create(function(ph,error) {
  c = callback;
  ph.createPage(function( page,err ) {  
    page.setContent( output );
    callback = c;
    setTimeout(function() {
       page.evaluate(function() {
         return document.getElementsByTagName('html')[0].innerHTML;
       }, function(html) {
         callback(html);
       });
    }, 5000);
  });
});

如何告诉 phantom “加载”/“执行”页面以便页面上的脚本运行?(我不想使用 page.open)

另外,内容是"<p>Page <strong>1</strong> content.</p><button id=\"page1_button\">button</button><script>document.getElementsByTagName('p').innerHTML = 'test';</script>"

4

1 回答 1

0

调用page.open,参见http://phantomjs.org/api/webpage获取参数。

第一个参数是 url,第二个参数是回调。

在对您的回调中,page.open您应该page.evaluate像之前尝试过的那样调用。

page.open("http://httpbin.org/get", function(status) {
    if (status == "success") {
        page.evaluate(function() {
            // ...
        });
    }
});
于 2013-06-12T21:53:04.313 回答