phantomjs + jquery: returns empty string - javascript

This webpage (test.html):
<p id="test">bla</p>
And this js code:
var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
console.log("Message: "+msg);
};
page.open("http://localhost/test.html", function(status) {
if ( status === "success" ) {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
console.log($("#test").text());
});
phantom.exit();
});
}
});
Result:
Message:
Does anybody know Why it doesn't display the text content of p when I run it with phantomjs?

try to use this one:
console.log($("#test").val());

Related

Parsing the Page

I wrote a small js script, I want to enter the site by login and password and parsing data birthday and name users on the page I need. But i do not know what i need to do further
var page = require('webpage').create();
phantom.cookiesEnabled = true;
page.open("http://www.facebook.com/login.php", function(status) {
if (status === "success") {
page.evaluate(function() {
document.getElementById("email").value = "login#mail.com";
document.getElementById("pass").value = "12345";
document.getElementById("u_0_1").click();
});
window.setTimeout(function() {
page.render("fb.png");
}, 5000);
page.open('https://www.facebook.com/friends/requests/?fcref=ff', function() {
window.setTimeout(function() {
page.render("fb2.png");
phantom.exit();
}, 5000);
});
}
});

unexpected result from PhantomJS

I am trying to write a PhantomJS script to check some security issues in some websites. I am adding the __domlog function to the window object, which should be called when i pass this URL for example:
https://domgo.at/cxss/example/1?payload=abcd&sp=x#1</iframe></style></script></object></embed></textarea><img src=x onerror=__domlog(0,0,51)><!--/*
This is the code:
function validateExploit(url, callback) {
console.log('Validating ..');
var page = require('webpage').create();
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
page.onCallback = function (data) {
result = data;
console.log(' -> Exploit successful');
};
page.onInitialized = function () {
page.evaluateJavaScript('function(){location.href = decodeURI(location.href);}');
page.customHeaders = { "X-XSS-Protection": "0" };
page.evaluate(function () {
window.__domlog = function (findingId, exploitId, tabId) {
window.callPhantom({
'success': true,
'findingId': findingId,
'exploitId': exploitId,
'tabId': tabId
});
};
});
};
page.open(url, function (status) {
page.close();
callback(result);
console.log(' -> Page closed. Exploit found: ' + result.success);
});
}
The problem is that this function isn't called, and the page.onConsoleMessage doesn't show any error. When i request the same link in Chrome, the __domlog function will be requested. As far as i know, phantomJS is based on Chrome, i wanted to ask if i did some error.

What is the right syntax for injecting Javascript with PhantomJS?

My goal is to inject:
javascript document.getElementById("pickupZip").value = "90049";
document.getElementById("refreshStoresForZipPop").click();
into a website with PhantomJS. This is all of the code on my inject.js file, and I get this in the console: SyntaxError: Expected an identifier but found "document" instead, so I know it is the wrong syntax, though I can't find a place that shows what the correct is. Here is my code for the PhantomJS file:
var webPage = require('webpage');
var page = webPage.create();
page.open('http://shop.advanceautoparts.com/p/purolator-classic-air-filter-a24278/5792304-P?navigationPath=L1*14934/', function(status) {
var success = phantom.injectJs('inject.js');
console.log(success);
if (status === "success") {
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {
page.render('advanceautoparts.png');
phantom.exit();
});
}
});
Use evaluate.
var webPage = require('webpage');
var page = webPage.create();
page.open('http://shop.advanceautoparts.com/p/purolator-classic-air-filter-a24278/5792304-P?navigationPath=L1*14934/', function(status) {
var success = phantom.injectJs('inject.js');
console.log(success);
if (status === "success") {
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {
page.evaluate(function(s) {
document.getElementById("pickupZip").value = "90049";
document.getElementById("refreshStoresForZipPop").click();
});
page.render('advanceautoparts.png');
phantom.exit();
});
}
});
Reference: http://phantomjs.org/api/webpage/method/evaluate.html

Phantomjs can't load network traffic when key down event sent

I tried to load the network traffic from a twitter page using Phantomjs, the problem is the keypress event didn't worked. here the code (netlog.js modified)
in other words I didn't see any output with sentEvent() unlike wothout sentEvent(), i want to scroll down the twitter page to get more network traffic.
var page = require('webpage').create(),
system = require('system'),
address;
if (system.args.length === 1) {
console.log('Usage: netlog.js <some URL>');
phantom.exit(1);
} else {
address = system.args[1];
page.onResourceRequested = function (req) {
console.log('requested: ' + JSON.stringify(req, undefined, 4));
};
page.onResourceReceived = function (res) {
console.log('received: ' + JSON.stringify(res, undefined, 4));
};
page.onLoadFinished = function(status) {
console.log('Status: ' + status);
};
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
}else{
page.sendEvent('keypress', page.event.key.Down);
}
phantom.exit();
});
}
Thanks for your Help.

TrifleJS quitting; "Unexpected ."

Can somebody tell me what is wrong with my syntax? While running this code in trifle JS it tells me that
. is unexpected at (2,4).
var page = require('webpage').create(),
page.open("http://www.phantomjs.org", function(status) {
if ( status === "success" ) {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
console.log(page.evaluate(function() {
return $("#intro").text();
}));
phantom.exit();
});
}
});
The problem is the ; at the end of the first line. This assumes that you define a variable in the next line which you don't do anymore. Exchange it with a semicolon:
var page = require('webpage').create();
page.open("http://www.phantomjs.org", function(status) {
//...
});

Categories