PhantomJS一个接一个地打开一页 [英] PhantomJS open one page after another

查看:85
本文介绍了PhantomJS一个接一个地打开一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此示例创建了一个phantomjs代码来登录网站。

I used this example to create a phantomjs code to login to website.

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

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

But I want to open another link from a button or go directly on it. How can I do it?

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

Here is a simpler example. Doesn't work...

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()是异步的,这就是为什么你需要在第一页完成后才打开下一页:

Very close, now combine your two snippets into one. page.open() is asynchronous which is why you need to open the next page only after the first one has finished:

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

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

To actually see the console.log() inside of page.evaluate() you will need to register to the page.onConsoleMessage event. There are more other events that are helpful when debugging.

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

Don't forget to add the protocol (http:// or file:///) to the URLs that you're opening. PhantomJS is a bit picky in that regard.

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

Instead of waiting a static amount of time (setTimeout()) until the next page is loaded after you do some action. You should make use of the page.onLoadFinished event. This is rather cumbersome to get right for navigation intensive scripts. Use CasperJS for longer scripts.

经常 Element.click()不起作用。 这个问题针对这些案例提供了许多解决方案。

Oftentimes Element.click() doesn't work. This question has many solutions for those cases.

这篇关于PhantomJS一个接一个地打开一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆