PhantomJS打开一个又一个页面

PhantomJS open one page after another

本文关键字:一个又一个 PhantomJS      更新时间:2023-09-26

我用这个例子创建了一个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);
  }
});

从此链接。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);
});

非常接近,现在将您的两个片段合并为一个。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);
});

要实际查看page.evaluate()内部的console.log(),您需要注册到page.onConsoleMessage事件。还有更多其他事件在调试时很有用。

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

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

Element.click()通常不起作用。这个问题对这些情况有很多解决办法。