Phantomjs not loading the correct url inside onResourceRequested - javascript

I have a simple script based off Phantomjs's Network Monitoring example that tries to log all the requests and the response headers associated with a particular website:
"use strict";
var page = require('webpage').create(),
system = require('system'),
address;
if (system.args.length === 1) {
console.log('Usage: getRequests.js <some URL>');
phantom.exit(1);
} else {
address = system.args[1];
console.log('The url entered is ' + address + '\n')
page.onResourceReceived = function(response) {
console.log('Received Request: ' + JSON.stringify(response.headers, undefined, 4) + '\n');
};
page.onResourceRequested = function (req) {
console.log('Request URL ' + JSON.stringify(req.url, undefined, 4) + "\n");
}
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.open(address);
}
However, when I try to run it, using ./phantomjs getRequests.js www.google.com (or any other webpage), it gives the error:
The url entered is www.google.com
Request URL "file:///home/myusername/scripts/www.google.com"
Unable to load resource (#1URL:file:///home/myusername/scripts/www.google.com)
Error code: 203. Description: Error opening /home/myusername/scripts/www.google.com: No such file or directory
Received Request: []
Basically, the url that I am entering on the terminal is being prepended with the path of the script file. The input that I am getting inside the address variable is perfectly fine i.e. google.com
Could anyone please help with this issue? I cannot understand why phantomjs maybe doing this. I am running Ubuntu 14.04 on VirtualBox. And the script was running fine before when I was just trying to output the requests for a particular page.
Thanks.

You'll probably laugh, but the correct way to use the script is
./phantomjs getRequests.js http://www.google.com

Related

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html"

I need help for this error, I saw that I'm the only one having this error but I can solved it. Here is my code :
I try to see the status of servers with NODE-PING, and this error appears.
Any idea?
import ping from '../../../ping';
var hosts = ['192.168.1.1', 'google.com', 'yahoo.com'];
console.log(hosts);
hosts.forEach(function(host){
ping.sys.probe(host, function(isAlive){
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
console.log(msg);
});
});

PhantomJS Issue with Loading Webpage: "Connection closed"'

I'm working with PhantomJS and everything that I've done works fine, but I don't know what happened bit time ago. The URL doesn't open in PhantomJS. The URL is domain and the code is as follows:
var page = require('webpage').create(),fs = require('fs'), args=require('system').args;
// var url = 'http://fotogena.co';
var url = 'https://www.procuraduria.gov.co/CertWEB/Certificado.aspx?tpo=1';
page.viewportSize = { width: 400, height: 400 };
page.open(url, function (status) {
if(status==='success'){
setTimeout(loadScripts, 1500)
}else
console.log(
"Error opening url \"" + page.reason_url
+ "\": " + page.reason
);
});
page.onResourceError = function(resourceError) {
console.error('-----',resourceError.url + ': ' + resourceError.errorString, 'faber acá toy');
phantom.exit(1);
};
function loadScripts(){
setTimeout(function(){
page.render('./probandoAntecedentes.jpg')
phantom.exit(1);
},1000)
}
The console displays the following:
https://www.procuraduria.gov.co/CertWEB/Certificado.aspx?tpo=1: Connection closed
I searched for possible solutions and put --ssl-protocol=any and other options, but they didn't work.
I'm using PhantomJS 2.1.1

Phantomjs cannot open a page that browser opens

I have a Phantomjs script that tries to open a url. phantomjs returns this error:
Unable to load resource (request ID:undefinedURL:http://foo.bar/tree/nav_value/27)
Error code: 203. Description: Error downloading http://foo.bar/tree/nav_value/27 - server replied: Not Found
But when I open the url http://foo.bar/tree/nav_value/27 with chrome browser, there's no problem and the page is loaded correctly!
This is the script:
// Read the Phantom webpage '#intro' element text using jQuery and "includeJs"
"use strict";
var page = require('webpage').create();
var system = require('system');
if (system.args.length != 2) {
console.log("please pass 2 argument")
}
var company_id = system.args[1]
console.log("c", company_id)
page.onConsoleMessage = function(msg) {
console.log("message", msg);
};
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (request ID:' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.onError = function(msg, trace) {
console.log("error", msg)
}
var nav_value;
page.open("http://foo.bar/tree/nav_value/27", 'post', 'username=navid&password=test', function(status) {
if (status === "success") {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
nav_value = parseInt($("#value").text());
});
phantom.exit(0);
});
} else {
phantom.exit(1);
}
});
EDIT:
Something odd happens. When I run this code with phantomjs on windows on another machine it works. But on Ubuntu it returns the error!
The url that phantomjs is trying to open is on the same server. (Ubuntu)
What is the problem?
Not sure this will help, but I have some ideas that helped me figure out problems with PhantomJS in the past.
First, as you say it works on another machine, you may want to test other versions of PhantomJS, by downloading the executable and specifying the path on your Python script. Version 1.9.8 helped me with bypassing some security restrictions in the past (I also left some settings in case it may interest).
driver = webdriver.PhantomJS(
executable_path='/path/to/the/downloaded/phantomjs19',
# you can specify args, such as:
service_args=[
'--ignore-ssl-errors=true',
'--ssl-protocol=any',
'--web-security=false',
],
# and also other capabilities:
desired_capabilities={
'phantomjs.page.settings.resourceTimeout': '5000',
'phantomjs.page.settings.userAgent': (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 "
"(KHTML, like Gecko) Chrome/15.0.87"
),
},
)
You may also try to see if upgrading Selenium helps.
pip install selenium --upgrade
Another idea that might help to understand what is happening is to try to print a screenshot and log the page source before the error happens. You can do it like:
# Set the window size to something appropriate for your tests.
driver.set_window_size(900, 800)
driver.save_screenshot('screen.png')
# Check if the page source matches your expectations.
with open('temp.html', 'w') as f:
f.write(driver.page_source)
Please, let me know if this helps!

Phantomjs does not open web page

I have started testing campus2020 site with casperjs (1.1.0-beta3) + phantomjs (1.9.8). And faced with the problem that site is not opening but instead tests just freeze. I have taken script example from phantomjs site:
var page = require('webpage').create();
page.open('http://informatik.uni-leipzig.de/campus2020', function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render('example.png');
}
phantom.exit();
});
It worked fine on other sites. I have tested opening the campus2020 site with phantomjs on several environments: win 7, Ubuntu 14.04, with ghostdriver and java selenium webdriver, with phantomjs which is run in selenium grid on RHEL 6.6. All this options failed. I have tried to add userAgent option and setTimeout. Nothing changed. Also I tried to open this site using testing framework based on selenium webdriver which used phantomjs but it worked in the same way - phantomjs initialized and then freezes. Any ideas how could be this issue solved?
Update
Now my code looks like these:
var page = require('webpage').create();
console.log("Page is going to be opened...")
page.open('http://informatik.uni-leipzig.de/campus2020/', function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render('example.png');
}
phantom.exit();
});
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
page.onError = function (msg, trace) {
console.log(msg);
trace.forEach(function(item) {
console.log(' ', item.file, ':', item.line);
})
}
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.onResourceTimeout = function(request) {
console.log('Response (#' + request.id + '): ' + JSON.stringify(request));
};
And no errors are shown.
I don't know why there is this issue, but you can update to PhantomJS 2 and it will work. There are not yet binaries for Linux, so you will need to build it yourself.
You will also need to update your CasperJS version, because the 1.1-beta3 doesn't support PhantomJS 2, but the master branch on GitHub does.
phantomjs --debug=true phantom_test.js

Testing Ajax using localhost

I am trying to test a simple Ajax script using Javascript and I'm not receiving a response. The PHP script is placed in the htdocs folder of the local server and the HTML page that includes the Ajax calls is placed on my desktop. A readyState of 4 is received from the server but the status returned is 0, not 200.
The error generated on Firefox state:
NS_ERROR_DOM_BAD_URI: Access to restricted URI denied.
Here is the section of code responsible for calling the Ajax object:
var myRequest = getXMLHttpRequest();
function callAjax() {
var url = "localhost/clock.php";
var myRandom = parseInt(Math.random()*99999999);
myRequest. open("GET", url + "?rand=" + myRandom, true);
myRequest.onreadystatechange = responseAjax;
myRequest.send(null);
}
function responseAjax() {
if(myRequest.readyState == 4) {
if(myRequest.status == 200) {
var now = new Date();
var localTime = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
var serverTime = myRequest.responseText;
document.getElementById("clock").innerHTML = "Server: " + serverTime + "<br />Local: " + localTime;
} else {
alert("An error has occurred: " + myRequest.statusText);
}
}
}
Can anyone offer insight to my problem?
OSX 10.8.5
MAMP version 2.1.3
You say your file is on the desktop. You cannot open a local file ( file:// ) and do ajax. You need to access the file through your Apache server ( http:// ).

Categories

Resources