1

The JavaScript code in page.evaluate() is not executed. Maybe you need to set the delay before performing?

var page = require("webpage").create(),
    system = require("system"),
    urls = "http://www.domaines.com",
    useragents = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36",
    w = 600,
    h = 800,
    cookie = phantom.addCookie({
        'name'     : 'uhash', 
        'value'    : '8bb0c9ebcb5781g55196a1ff08c41b4d',
        'domain'   : '.domaines.com',
        'path'     : '/',                
        'httponly' : false,
        'secure'   : false
    });
page.viewportSize = { width: w, height: h };
page.settings.userAgent = useragents;
page.open(urls, function(status){
    if(status !=='success'){
        phantom.exit();
    }else{
        page.cookie;
        page.evaluate(function(){
            function load(){
                var links = document.querySelectorAll('a.link');
                Array.prototype.forEach.call(links, function(e){ 
                    e.click();
                });
            }
            document.addEventListener('DOMContentLoaded', load);
        });

        window.setTimeout(function () {
            page.render('s.png');
            phantom.exit();
        }, 200);
    }
});
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
excellproj
  • 13
  • 1
  • 3

1 Answers1

0

PhantomJS's page.open() is an asynchronous function. Its callback is called at the earliest when the response to the page request was fully transmitted and parsed. It's not clearly defined when exactly the callback is called, but from experience I would say it is always called as soon as all immediate resources are also loaded (not async resources).

This means that the callback is called long after the DOMContentLoaded event was triggered on the page. Which in turn means that if you register to the DOMContentLoaded after it was fired, your event handler will never fire.

Now it looks like this:

page.evaluate(function(){
    var links = document.querySelectorAll('a.link');
    Array.prototype.forEach.call(links, function(e){ 
        e.click();
    });
});

The problem is that element.click() doesn't work on most pages. The question PhantomJS; click an element has multiple solutions for this predicament. You do it like this:

page.evaluate(function(){
    var links = document.querySelectorAll('a.link');
    Array.prototype.forEach.call(links, function(e){
        var ev = document.createEvent('MouseEvents');
        ev.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, 
                false, false, false, false, 0, null);
        e.dispatchEvent(ev);
    });
});
Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222