zeronine09
zeronine09

Reputation: 23

PhantomJS can't capture

PhantomJS runs until a while ago, but suddenly it does not work normally.

I tried to capture this site, but the images are not loaded and the screenshot is saved. For those sites, there is Data to be imported into Ajax,

During site loading:

TypeError: undefined is not an object (evaluating 'naver.main.myteam.init').

It seems that this error does not cause the syntax to call Data in Ajax.

Please see the attached code and error text. I will attach the screenshot file and the screenshots of the results I want.

var resourceWait = 300,
    maxRenderWait = 10000,
    url = 'https://m.naver.com';

var page = require('webpage').create(),
    count = 0,
    forcedRenderTimeout,
    renderTimeout;

page.settings.javascriptEnabled = true;
page.settings.webSecurityEnabled = false;
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1';

function doRender(){
    page.render('naver-mobile.png');
    phantom.exit();
}

page.onResourceRequested = function(req){
    count += 1;
    console.log('> ' + req.id + ' - ' + req.url);
    clearTimeout(renderTimeout);
};

page.onResourceReceived = function(res){
    if(!res.stage || res.stage === 'end'){
        count -= 1;
        console.log(res.id + ' ' + res.status + ' - ' + res.url);
        if(count === 0){
            renderTimeout = setTimeout(doRender, resourceWait);
        }
    }
};

page.open(url, function(status){
    if(status !== "success"){
        console.log('Unable to load url');
        phantom.exit();
    }else{
        forcedRenderTimeout = setTimeout(function(){
            console.log(count);
            doRender();
        }, maxRenderWait);
    }
});

Failed screenshot file:

failed screenshot file

I want result screenshot file:

I want result screenshot file

Upvotes: 1

Views: 331

Answers (1)

user4535610
user4535610

Reputation:

You can't do that because PhantomJS has not window.Audio function:

ReferenceError: Can't find variable: Audio

And it causes many Errors..

You need to use slimerjs - it has almost the same api, and works on top of FireFox, you also need to install xvfb to work with it.

# xvfb Linux Example:
pkill [X,x]vfb; pkill nw; Xvfb :1 -screen 1 1440x900x24 >/dev/null 2>&1 &

# Adding the symbolic links:
ln -sf /path/to/firefox /usr/local/bin  -v; \
ln -sf /path/to/slimerjs /usr/local/bin  -v

export DISPLAY=:1.1; slimerjs test.js >>/dev/stdout

I've tested the following SlimerJS script, it works perfectly:

function on_init (page){
page.viewportSize = {width:1024,height:768}
page.evaluate(function (){
screen = {width:1024,height:768,availWidth:1024,availHeight:768};
innerWidth=1024;  innerHeight=768;   outerWidth=1024;  outerHeight=768;
window.navigator = {
plugins: {length: 2, 'Shockwave Flash': {name: 'Shockwave Flash', filename: '/usr/lib/flashplugin-nonfree/libflashplayer.so', description: 'Shockwave Flash 11.2 r202', version: '11.2.202.440'}},
mimeTypes: {length: 2, "application/x-shockwave-flash": {description: "Shockwave Flash", suffixes: "swf", type: "application/x-shockwave-flash", enabledPlugin: {name: 'Shockwave Flash', filename: '/usr/lib/flashplugin-nonfree/libflashplayer.so', description: 'Shockwave Flash 11.2 r202', version: '11.2.202.440'}}},
appCodeName: "Mozilla",
appName: "Netscape",
appVersion: "5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36",
cookieEnabled: 1,
languages: "en-US,en",
language: "en",
onLine: 1,
doNotTrack: null,
platform: "Linux x86_64",
product: "Gecko",
vendor: "Google Inc.",
vendorSub: "",
productSub: 20030107,
userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36",
geolocation: {getCurrentPosition: function getCurrentPosition(){},watchPosition: function watchPosition(){},clearWatch: function clearWatch(){}},
javaEnabled: function javaEnabled(){return 0} };});};

var page = require('webpage').create(); page.onInitialized=function(){on_init(page)}

page.open('https://m.naver.com',function(status){
setTimeout(function(){
page.render('naver-mobile.png');phantom.exit();console.log('Done!');
},5000);
});

Upvotes: 1

Related Questions