Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PhantomJS-默认情况下使用LocalStorage打开页面_Javascript_Phantomjs_Local Storage - Fatal编程技术网

Javascript PhantomJS-默认情况下使用LocalStorage打开页面

Javascript PhantomJS-默认情况下使用LocalStorage打开页面,javascript,phantomjs,local-storage,Javascript,Phantomjs,Local Storage,在JavaScript DOM操作发生后,我使用获取生成的网页源代码。此网页只有一个,没有其他内容 重要信息:此网页使用浏览器的本地存储生成页面 我想在打开页面之前更改PhantomJS中的本地存储 App.js: var page = require('webpage').create(); page.open("https://sample.com") setTimeout(function(){ // Where you want to save it page.

在JavaScript DOM操作发生后,我使用获取生成的网页源代码。此网页只有一个
,没有其他内容

重要信息:此网页使用浏览器的
本地存储
生成页面

我想在打开页面之前更改PhantomJS中的本地存储

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仅在您打开该域上的页面时可用。你可以

  • 在您感兴趣的域上打开一些URL
  • 根据需要更改
    localStorage
  • 在同一域上打开您的目标URL
  • 这可以是这样的:

    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中不打开整页。您还可以使用带有一些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)
    });    
    

    我在朋友的帮助下解决了我的问题。我的解决办法是:

    注意:我为完整加载页面设置了超时10000(10秒)


    您可以在
    page.evaluate()中设置一些
    localStorage
    属性。你试过了吗?有什么问题吗?@ArtjomB。感谢您的编辑和评论。我的问题是在页面之前为浏览器设置本地存储。打开(url)如何为尚未访问的页面设置本地存储?@ArtjomB。谢谢你edit@epascarello我尝试在打开页面之前设置页面的本地存储。
    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)
        });    
    });