I'm new to Phantomjs.I have tried to load the page using the below code.But the given page is not loading while running this.
console.log('Loading a web page');
var page = require('webpage').create();
var url = 'http://www.phantomjs.org/';
page.open(url, function (status) {
//Page is loaded!
phantom.exit();
});
Your code is correct but you have to do something before phantom.exit();
See all examples here.
Let's capture a web page as a screenshot :
console.log('Loading a web page');
var page = require('webpage').create();
var url = 'http://www.phantomjs.org/';
page.open(url, function (status) {
//Page is loaded!
page.render('phantomjs.png');
phantom.exit();
});
Related
I'm trying to render a webpage, but it doesn't work with some pages. It works with my homepage, and with google, but not with the one shown in the script. How do I work around that?
script:
var page = require('webpage').create();
page.open('https://login.microsoftonline.com', function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render('micros.png');
}
phantom.exit();
});
I have a PhantomJs script in which I create a new wepage, inject jQuery into it and scrape a list of URL from it. After that I call a function passing the list of URL and create a new webpage for each one and try to recover certain information from it
var pageGlobal = require('webpage');
function createPage(){
var page = pageGlobal.create();
page.onAlert = function(msg) {
console.log(msg);
};
return page;
}
var page=createPage();
page.open('http://www.example.com/', function(status){
if ( status === "success" ) {
page.injectJs('jquery-1.6.1.min.js');
var urlList=page.evaluate(
function(){
var urlList=[];
window.console.log = function(msg) { alert(msg) };
$("td.row1>a").each(function(index, link) {
var link=$(link).attr('href');
urlList.push(link);
});
return urlList;
});
processUrlList(urlList);
}
});
function processUrlList(urlList){
for(i=0;i<urlList.length;i++){
var currentPage=createPage();
currentPage.open("http://www.example.com"+urlList[i], function(status){
if ( status === "success" ) {
if(currentPage.injectJs('jquery-1.6.1.min.js')===false){
console.log("Error en la inyeccion");
}
currentPage.evaluate(function() {
window.console.log = function(msg) { alert(msg) };
console.log("Evaluating");
$("showAdText").each(function(index, link) {
//Capture information about the entity in this URL
})
});
}
});
}
}
The problem is in the processUrlList function the injection of jQuery always fail returning false. Would it be a problem to create two or more page objects instead of reusing only one? What could be happening here?
Is there a way to give Phantom.js HTML code instead of a URL to render?
HTML URL Example
var page = require('webpage').create();
page.open('http://github.com/', function() {
page.render('github.png');
phantom.exit();
});
Desired HTML markup example
var page = require('webpage').create();
page.open('<html><head><style>SOME CSS</style></head><body><div>SOME TEXT AND IMAGES</div></body></html>',
function() {
page.render('github.png');
phantom.exit();
}
);
Yes, there is a way of doing this:
var page = require('webpage').create();
page.viewportSize = { width: 800, height : 600 };
page.content = '<html><head><style>SOME CSS</style></head><body><div>SOME TEXT AND IMAGES</div></body></html>';
page.evaluate(function() {
// your logic here
});
page.render('github.png');
phantom.exit();
Below code is download web page it work fine but i want to save i put code for write text file honestly i have no idea how i can do this to save file
var url = 'http://stackoverflow.com';
var page = require('webpage').create();
page.open(url, function(status) {
if (status === 'success') {
var html = page.evaluate(function() {
return document.documentElement.outerHTML;
});
console.log(html);
}
var fs = require('fs');
try {
fs.write("C:\phantomjs\\qhxpZ.txt", "Message to be written to the file", 'w');
} catch(e) {
console.log(e);
}
phantom.exit();
});
So, for completeness, the solution to the issue at hand should look something like:
var url = 'http://stackoverflow.com';
var fs = require('fs');
var page = require('webpage').create();
page.open(url, function(status) {
if (status === 'success') {
var html = page.evaluate(function() {
return document.documentElement.outerHTML;
});
try {
fs.write("C:\\phantomjs\\qhxpZ.txt", html, 'w');
} catch(e) {
console.log(e);
}
}
phantom.exit();
});
just replace "Message to be written to the file" with html and file will be saved.
fs.write("C:\phantomjs\\qhxpZ.txt", "Message to be written to the file", 'w');
I am developing a chrome extension and I have an iframe which I use in my extension. This is what I do with my iframe
"When I drag and drop a image to my iframe I handle the drop event in one of the content scripts and pass that function call my extension code. There I create a xmlhttprequest object and then send the URL of the image to a php file in my server."
This is what is happening. I get a readyState of "4" but there is no POST request going out of my browser. I checked with the "NETWORK" tab in the browser but there is no POST request going out of the browser (I have listed my site in the permissions section of the manifest file).
This is my code --.>
JScript.js(One of the content scripts )
drop: function(event, ui) {
var imgurl=$(ui.draggable).attr('src');
imgurl="IMGURL="+imgurl;
_post("www.somedomain.come/testing.php",imgurl,function(result){ alert("success")});
}
This is my proxy in the same content script-->
_post = function(url, data, callback)
{
console.log("sending post");
chrome.extension.sendRequest({
msgType:'post',
data: data,
url:url
}, function(response){
alert(response);
});
}
This my OnRequest function handler in background.html -->
chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
if (request.msgType === 'post') {
alert("Now in OnRequest function");
// console.log("Now in Onrequest Function");
alert("Url: "+request.url + "\n Data : "+ request.data);
ajaxcallingfunction(request);
alert("completed the ajax call");
sendResponse("success");
}
});
var ajaxcallingfunction = function(request){
var xhr = new XMLHttpRequest();
xhr.open("POST",request.url, false);
xhr.onreadystatechange = function(){
alert(xhr.readyState);
if (xhr.readyState == 4) {
alert(xhr.readyState);
}
}
xhr.send(request.data);
alert("after xhr call");
};
You have http:// in front of your url, right?
xhr.readyState doesn't tell much, it just means that it is done. Check out what's inside xhr.status, it would contain error code. If everything is ok it should be 200:
xhr.onreadystatechange = function(){
if (xhr.readyState == 4) {
alert(xhr.status);
}
}