【问题标题】:PhantomJS open one page after anotherPhantomJS 一页一页地打开
【发布时间】:2015-06-21 20:51:45
【问题描述】:

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

});

【问题讨论】:

  • 请注意,PhantomJS 的执行环境与 node 不同。 node 和 PhantomJS 之间有桥梁,但它们的写法略有不同。

标签: javascript phantomjs


【解决方案1】:

非常接近,现在将您的两个 sn-ps 合二为一。 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() 通常不起作用。 This question 有很多针对这些情况的解决方案。

【讨论】:

  • 好的。这已经奏效了,但是如果我想在谷歌搜索框中输入一些内容并按下按钮进行搜索怎么办。然后我得到渲染的搜索结果?
  • @macroscripts Google 是一个开始尝试 PhantomJS 的非常糟糕的网站。但是您可以使用page.evaluate() 来更改输入字段的值,并使用click 来更改搜索按钮。您也可以使用page.sendEvent() 填写该字段并希望为 PhantomJS 启用即时结果。
  • 我以google为例。我在网站上做这个代码。它有不同的域。我必须从 sub1.domain.com 登录并访问 sub2.domain.com 。我想从中获取一些数据。你给我的代码有效但不完全。它到达 sub1 但没有正确登录。它转到 sub2 但我没有看到我的用户名登录。我将粘贴我稍后使用的示例。
  • 如果您对此有疑问,请提出新问题。另外,请注册resource.errorpage.errorremote.messagecasper.page.onResourceTimeout 活动(Example)。可能有错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 2016-08-01
  • 2014-08-19
  • 2016-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多