How to use phantoms?

I would like to learn phantomjs, but I can not find a good tutorial. I have 2 questions:

  • where the problem is in the following code (you need to write the button label and write to the file):

    var page = require('webpage').create();
    var fs = require('fs');
    
    page.onConsoleMessage = function(msg) {
        phantom.outputEncoding = "utf-8";
        console.log(msg);
    };
    
    page.open("http://vk.com", function(status) {
        if ( status === "success" ) {
            page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
                page.evaluate(function() {
                    var str = $("#quick_login_button").text();
                    f = fs.open("ololo.txt", "w");
                    f.writeLine(str);
                    f.close();
                    console.log("done");
                });
                phantom.exit();
            });
        }
    });
    
  • What phantomjs tutorial can you recommend me? (not from the official site)

+5
source share
1 answer

Because execution is isolated, the web page does not have access to phantom objects.

var page = require('webpage').create();
var fs = require('fs');

page.onConsoleMessage = function(msg) {
    phantom.outputEncoding = "utf-8";
    console.log(msg);
};

page.open("http://vk.com", function(status) {
    if ( status === "success" ) {
        page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
            var str = page.evaluate(function() {
                return $("#quick_login_button").text();        
            });
            f = fs.open("ololo.txt", "w");
            f.writeLine(str);
            f.close();
            console.log("done");

            phantom.exit();
        });
    }
});

PhantomJS comes with many attached examples. Take a look here .

+2
source

All Articles