2017-10-19 120 views
0

我試圖存儲cookie後,我從website.But,但是,當我創建一個全局變量,我試圖將它存儲在變量它返回未定義或當我嘗試創建一個文件與它的餅乾,它會返回未定義,但我可以用console.log打印。我怎樣才能存儲與節點幻像餅乾?

我的代碼:

var cookies; 

phantom.create(['--ignore-ssl-errors=yes', '--load-images=no', '--ssl- 
protocol=any']).then(function(ph) { 
ph.createPage().then(function(page) { 

page.property("onConsoleMessage", function(message) { 
    console.log(message); 
}); 

page.property('onResourceReceived', function(response) { 
     console.log(phantom.cookies); //Show the page cookies 
     cookies = phantom.cookies; 
     fs.write(CookieJar, JSON.stringify(phantom.cookies), "w"); //its not creating a file 
}).then(function() { 
     console.log('Cookies'); 
     console.log(phantom.cookies); //Equals to undefined 
     console.log(cookies); //Equals to undefined 
});; 

page.open('https://stackoverflow.com').then(function(status) { 
    if(status == 'success') { 
    console.log('success'); 
    } 
}); 

}); 
}); 

我怎樣才能將這些Cookie存儲上的文件,怎麼做呢?

回答

0

您可以查看可能的用例的測試。這是一個example

await page.addCookie({ 
    name: 'Valid-Cookie-Name', 
    value: 'Valid-Cookie-Value', 
    domain: 'localhost', 
    path: '/foo', 
    httponly: true, 
    secure: false, 
    expires: new Date().getTime() + (1000 * 60 * 60), 
});