PhantomJS一个接一个地打开一页

mac*_*pts 10 javascript phantomjs

我用这个例子来创建一个phantomjs代码来登录网站.

var page = require('webpage').create();
page.open("http://www.facebook.com/login.php", function(status) {

  if (status === "success") {
    page.onConsoleMessage = function(msg, lineNum, sourceId) {
      console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
    };
    page.evaluate(function() {
      console.log('hello');
      document.getElementById("email").value = "email";
      document.getElementById("pass").value = "password";
      document.getElementById("u_0_1").click();
      // page is redirecting.
    });
    setTimeout(function() {
      page.evaluate(function() {
        console.log('haha');
      });
      page.render("page.png");
      phantom.exit();
    }, 5000);
  }
});
Run Code Online (Sandbox Code Playgroud)

从这个链接. https://gist.github.com/ecin/2473860

但我想从按钮打开另一个链接或直接在它上面.我该怎么做?

这是一个更简单的例子.不起作用......

var page = require('webpage').create();
var url = "www.example.com";

page.open(url, function (status) {

    setTimeout(function () {
        page.evaluate(function () {
            console.log('haha');
        });
        page.render("example.png");
        phantom.exit();
    }, 5000);

});



var url = "www.google.com";

page.open(url, function (status) {

    setTimeout(function () {
        page.evaluate(function () {
            console.log('haha');
        });
        page.render("google.png");
        phantom.exit();
    }, 5000);

});
Run Code Online (Sandbox Code Playgroud)

Art*_* B. 9

非常接近,现在将您的两个片段合并为一个.page.open()是异步的,这就是为什么你需要在第一页完成之后才打开下一页:

var page = require('webpage').create();
var url = "http://www.example.com";

page.onConsoleMessage = function(msg, lineNum, sourceId) {
    console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};

page.open(url, function (status) {
    page.onConsoleMessage = function(msg, lineNum, sourceId) {
        console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
    };
    page.evaluate(function() {
        document.getElementById("email").value = "email";
        document.getElementById("pass").value = "password";
        document.getElementById("u_0_1").click();
        // page is redirecting.
    });

    setTimeout(function () {
        page.evaluate(function () {
            console.log('haha');
        });
        page.render("example.png");


        var url = "http://www.google.com";

        page.open(url, function (status) {
            setTimeout(function () {
                page.evaluate(function () {
                    console.log('haha');
                });
                page.render("google.png");
                phantom.exit();
            }, 5000);
        });
    }, 5000);
});
Run Code Online (Sandbox Code Playgroud)

要实际看到你的console.log()内部page.evaluate()需要注册到该page.onConsoleMessage事件.调试时还有其他更多有用的事件.

不要忘记将协议(http://或file:///)添加到您要打开的URL中.PhantomJS在这方面有点挑剔.

而不是等待静态的时间(setTimeout()),直到您执行某些操作后加载下一页.你应该利用这个page.onLoadFinished事件.这对于导航密集型脚本来说是相当麻烦的.使用CasperJS可以获得更长的脚本.

通常Element.click()不起作用.这个问题有许多解决方案.