如何将 CasperJS 调试结果打印到文本文件

How to print CasperJS debugging results to text file

我看不懂命令提示符中的调试问题。我可以将它发送到我的 PhantomJS 或 CasperJS 脚本中的文本文件吗?

这是我的 python 代码;

import os
import subprocess

#proxies = {'https': 'http://wmhproxy2:8080'}
APP_ROOT = os.path.dirname(os.path.realpath(__file__))

CASPER = "C:/casperjs/bin/casperjs"

#SCRIPT = os.path.join(APP_ROOT,'unicorn.js')
SCRIPT = os.path.join(APP_ROOT,'unicorn.js')

params = CASPER +' '+ SCRIPT
paper = subprocess.check_output(params,shell=True)
rock = paper.text
salesforce = open('S:/N/salesforce2.txt','w')
salesforce.write(write)
print(subprocess.check_output(params,shell=True))

CasperJS 脚本:

var casper = require('casper').create({
  verbose: true,
  logLevel: "debug"
});

var x = require('casper').selectXPath;
casper.options.waitTimeout = 7000;

casper.start('http://www.click2houston.com/',function(){
  this.echo(this.getTitle());
}).viewport(1200,1000);

casper.run();

是的,您可以将日志附加到 CasperJS 脚本中的文件中。

CasperJS 有一个内部事件管理,它在您的事件处理程序中公开 log event. A simple object is passed into the event handler which also contains the log message. If you want a different formatting of the logs, then you need to do it yourself or implement a part of the log code

var fs = require("fs");

fs.write("mylogfile.log", "", "w"); // overwrite log file
casper.on("log", function(entry){
    fs.write("mylogfile.log", entry.message + "\n", "a");
});

CasperJS 构建于 PhantomJS 之上,因此您可以直接使用 PhantomJS' file system module. Of course you can also pass the log file as a commandline option into the script and get it out through casper.cli.