Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhantomJS / Javascript: write to file instead of to console

Tags:

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.

like image 853
user984003 Avatar asked Mar 27 '13 08:03

user984003


People also ask

Is PhantomJS deprecated?

PhantomJS is a discontinued headless browser used for automating web page interaction.

What is the alternative of PhantomJS?

Selenium, Electron, Protractor, wkhtmltopdf, and SlimerJS are the most popular alternatives and competitors to PhantomJS.

How do you use 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.


2 Answers

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. 
like image 149
Arun Avatar answered Sep 27 '22 16:09

Arun


So I figured it out:

>phantomjs.exe file_to_run.js > my_log.txt 
like image 20
user984003 Avatar answered Sep 27 '22 15:09

user984003