我正在尝试使用 spawn
在Windows 7上使用Node.js打开Google Chrome。除了设置user-data-dir
外,它似乎适用于所有参数。但是,--user-data-dir
直接从命令行工作,并且如果我从Node调用.cmd文件,则可以通过它进行参数。
Chrome在后台打开(在任务管理器中显示),但是浏览器窗口没有打开,我必须手动杀死任务管理器中的过程才能结束它。
我没有任何错误或任何迹象表明问题是什么。
这是我的节点代码:
var spawn = require('child_process').spawn;
var exe = 'C:\Program Files\Google\Chrome\application\chrome.exe';
//this does not work and gives no errors just starts chrome in the background but fails to open browser window
// var args = [
// '--user-data-dir=C:\Windows\Temp\testingdir',
// 'http://www.google.com'
// ];
//actual output from running this through a .cmd file and echoing out the arguments
// --user-data-dir=C:WindowsTemptestingdir http://www.google.com
//running the above arguments through a .cmd file from nodejs or manually running them in the cli works
//this works from nodejs, other arguments work too, just not the --user-data-dir argument
// var args = ['http://www.google.com'];
chrome = spawn(exe, args);
chrome.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
chrome.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
chrome.on('exit', function (code) {
console.log('child process exited with code ' + code);
// chrome.kill();
});
您可以在评论的代码中看到,只要我不尝试更改用户数据data-dir,它就可以工作。我尝试了不同的临时目录,并在临时目录中设置了对"每个人"的完整访问权限,以防万一。
更新:我刚刚从v0.8.1更新到最新的v0.8.14,现在当它失败时, on('exit', function())
实际上返回了代码。
child process exited with code 20
如何查找此代码以找出它的含义?
更新2 出口代码20我认为只是一次fluke,因为我不能再重现它了。试图测试不同的事情有点障碍,因为我必须记住每次在任务管理器中杀死Chrome流程,以确保我获得相同的结果。因此,它恢复了与以前相同的结果,没有错误,但没有镀铬窗口,并且必须手动杀死一些镀铬过程。
更新3 我只是将此脚本移到了Ubuntu,并更改了EXE并进行了反映以反映不同的文件结构,并且在Ubuntu上起作用。
exe = "/opt/google/chrome/google-chrome"
和
args = [
"http://www.google.com",
"--user-data-dir=/home/lotus/testdir"
];
因此,我可以确认这与Windows上的Chrome特别相关。
http://www.hiteksoftware.com/mize/knowledge/articles/049.htm
20 The system cannot find the device specified.
尽管