From PhantomJS, how do I write to a log instead of to the console?
In the examples https://github.com/ariya/phantomjs/wiki/Examples, it always (in the ones I have looked at) says something like:
console.log('some stuff I wrote');
This is not so useful.
PhantomJS is a discontinued headless browser used for automating web page interaction.
Selenium, Electron, Protractor, wkhtmltopdf, and SlimerJS are the most popular alternatives and competitors to PhantomJS.
For Windows Download the zip file, unpack it and you will get an executable phantom.exe. Set the PATH environment variable to the path of phantom.exe file. Open a new command prompt and type phantomjs –v. It should give you the current version of PhantomJS that is running.
The following can write contents to the file directly by phantomjs:
var fs = require('fs'); try { fs.write("/home/username/sampleFileName.txt", "Message to be written to the file", 'w'); } catch(e) { console.log(e); } phantom.exit();
The command in the answer by user984003 fails when there is some warning or exceptions occurred. And sometimes does not fall into our specific requirements because in some codebase I am getting the following message always which will also be logged to that file.
Refused to display document because display forbidden by X-Frame-Options.
So I figured it out:
>phantomjs.exe file_to_run.js > my_log.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With