【问题标题】:Deleting cookies file after CasperJS script finishedCasperJS脚本完成后删除cookies文件
【发布时间】:2014-09-04 01:12:20
【问题描述】:

我想使用fs 库在我的脚本开头创建一个文件并在结尾删除它。 cookies.txt 文件在脚本开始时创建,但在脚本结束时不会被删除..

phantom.cookiesEnabled = true;
var fs = require('fs');
var cookies = JSON.stringify(phantom.cookies);
fs.write("cookies.txt", cookies, 777); // THIS WORKS !!

casper.start('http://google.fr/', function(){
    this.viewport(320, 480);
});
// my script ....
casper.then(function(){
    fs.remove("cookies.txt"); // THIS NOT WORKS !!
});
casper.run(function() {
    this.test.done();
    this.exit();
});

我写的启动脚本的命令

casperjs test lbc.js --cookies-file=cookies.txt --web-security=no

【问题讨论】:

    标签: javascript file phantomjs casperjs fs


    【解决方案1】:

    它有效,但它发生得如此之快,以至于你看不到它真的有效。

    您使用cookies-file 选项启动casper(请参阅docs)。这意味着您希望 cookie 在脚本退出后保留,以便在下次调用时使用它们。

    当您exit casper(和幻象)时,cookie 文件会写入磁盘,但在您使用fs.remove("cookies.txt"); 删除文件后会发生这种情况。所以文件实际上是被删除然后重写的。

    问题是,你想做什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-30
      • 2016-07-27
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 2016-11-07
      相关资源
      最近更新 更多