PhantomJS- open page with LocalStorage by default

六月ゝ 毕业季﹏ 提交于 2019-12-28 23:08:12

问题


I'm using PhantomJS to get the generated source of a web page after JavaScript DOM manipulations have taken place. This web page has JUST a <body> and nothing else.

Important: This web page uses browser's localStorage to generate the page.

I want to change LocalStorage in PhantomJS before opening the page.

App.js:

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

page.open("https://sample.com")
setTimeout(function(){
    // Where you want to save it    
    page.render("screenshoot.png")  
    // You can access its content using jQuery
    var fbcomments = page.evaluate(function(){
        return $("body").contents().find(".content") 
    }) 
    phantom.exit();
}, 1000)

回答1:


localStorage for a particular domain is only available when you open a page on that domain. You can

  1. open some URL on the domain you're interested in,
  2. change localStorage according to your needs,
  3. open your target URL on the same domain.

This can look like this:

page.open("https://sample.com/asdfasdf", function(){
    page.evaluate(function(){
        localStorage.setItem("something", "whatever");
    });

    page.open("https://sample.com", function(){
        setTimeout(function(){
            // Where you want to save it    
            page.render("screenshoot.png")  
            // You can access its content using jQuery
            var fbcomments = page.evaluate(function(){
                return $("body").contents().find(".content") 
            }) 
            phantom.exit();
        },1000)
    });    
});

It's also possible not to open a full page in step 1. You can also use dummy page with some URL.

page.setContent("", "https://sample.com"); // doesn't actually open any page

page.evaluate(function(){
    localStorage.setItem("something", "whatever");
});

page.open("https://sample.com", function(){
    setTimeout(function(){
        // Where you want to save it    
        page.render("screenshoot.png")  
        // You can access its content using jQuery
        var fbcomments = page.evaluate(function(){
            return $("body").contents().find(".content") 
        }) 
        phantom.exit();
    }, 1000)
});    



回答2:


I solved my problem with Friend's Help. My solution is:

Notice: I set Timeout 10000(10 seconds) for complete load page.

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

page.open("https://sample.com", function(){
    page.evaluate(function(){
        var i = 0,
        oJson = jsonData,
        sKey;
        localStorage.clear();

        for (; sKey = Object.keys(oJson)[i]; i++) {
            localStorage.setItem(sKey,oJson[sKey])
        }
    });

    page.open("https://sample.com", function(){
        setTimeout(function(){
         page.render("screenshoot.png") 
            // Where you want to save it    
           console.log(page.content); //page source
            // You can access its content using jQuery
            var fbcomments = page.evaluate(function(){
                return $("body").contents().find(".content") 
            }) 
            phantom.exit();
        },10000)
    });    
});


来源:https://stackoverflow.com/questions/35267191/phantomjs-open-page-with-localstorage-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!