PhantomJS- open page with LocalStorage by default

后端 未结 2 1454
别跟我提以往
别跟我提以往 2020-12-11 05:43

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 and no

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 05:46

    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)
        });    
    });
    

提交回复
热议问题