Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use phantomjs?

Tags:

phantomjs

I would like to learn phantomjs, but i can`t find good tutorial. I have 2 questions:

  1. where is problem in following code (need to capture label of button and write to 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();
            });
        }
    });
    
  2. what tutorial in phantomjs you can advice to me? (not from official site)

like image 862
Oleksandr H Avatar asked Nov 04 '22 03:11

Oleksandr H


1 Answers

Because execution is sandboxed, the web page has no access to the 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 a lot of included examples. Take a look here.

like image 128
Cybermaxs Avatar answered Dec 19 '22 23:12

Cybermaxs