PhantomJS-默认情况下打开LocalStorage页面 [英] PhantomJS- open page with LocalStorage by default

查看:511
本文介绍了PhantomJS-默认情况下打开LocalStorage页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PhantomJS 在JavaScript DOM操作发生后获取生成的网页源。这个网页只有一个< body> ,没有别的。

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.

重要 :此网页使用浏览器的 localStorage 生成页面。

我想在打开页面之前更改PhantomJS中的LocalStorage。

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)


推荐答案

特定域的localStorage仅在您在该域上打开页面时可用。您可以

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


  1. 在您感兴趣的域名上打开一些网址,

  2. 更改 localStorage 根据您的需要,

  3. 在同一个域上打开您的目标网址。

  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.

这可能如下所示:

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

也可以不在步骤1中打开整页。您也可以使用虚拟页面网址。

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

这篇关于PhantomJS-默认情况下打开LocalStorage页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆