Scraping value in ajax-loaded content results in white page [duplicate] - javascript

This question already has an answer here:
CasperJS bind issue
(1 answer)
Closed 8 years ago.
I'm having troubles trying to scrap the price of this webpage: http://www.voyages-bateau.com
It looks easy but any of the scraping services/tools I try seems to work with this page. Its content is loaded via ajax and the price appear later with an animation. I try the wait() and waitFor() helpers with no luck...
Here's the code I used to fetch this bad boy:
var casper = require('casper').create({
verbose: true,
logLevel: "debug"
});
casper.start('http://voyages-bateau.com', function() {
console.log(this.getHTML()); // no content loaded yet
});
casper.waitForSelector('//*[#id="WRchTxt0-3cb"]/h2[3]/span', function() {
var res = this.getHTML();
this.echo(res);
});
casper.run();
All I got is the error: "Wait timeout of 5000ms expired, exiting.". Any ideas ?

The main issue is that PhantomJS 1.x has no support for Function.prototype.bind. The workaround can be found here: CasperJS bind issue. Because of this none of the JavaScript runs, since there is a page error and you see nothing, because it is a JS driven page.
You can verify this by registering to the page.error event:
casper.on("page.error", function(pageErr){
this.echo("page.err: " + JSON.stringify(pageErr));
});
This yields this
page.err: "TypeError: 'undefined' is not a function (evaluating 'b.bind(a)')"
page.err: "TypeError: 'undefined' is not a function (evaluating 'c.bind(null,\"margin\")')"
page.err: "TypeError: 'undefined' is not a function (evaluating 'RegExp.prototype.test.bind(/^(data|aria)-[a-z_][a-z\\d_.\\-]*$/)')"
Which otherwise doesn't pop up with debug output or verbose enabled.
The other issue is that you forgot to use the XPath utility for XPaths:
var x = require('casper').selectXPath;
somewhere at the top and later:
casper.waitForSelector(x('//*[#id="WRchTxt0-3cb"]/h2[3]/span'), function() {
var res = this.getHTML();
this.echo(res);
});
Without the XPath utility, it tries to interpret this as a CSS selector. Since you have verbose: true, you have to have seen
[error] [remote] findAll(): invalid selector provided "//*[#id="WRchTxt0-3cb"]/h2[3]/span":Error: SYNTAX_ERR: DOM Exception 12

Related

What is the equivalent command to close for chrome.runtime.openOptionsPage?

In a Chrome extension what is the equivalent of close to chrome.runtime.openOptionsPage? I have tried:
window.close();
and
chrome.runtime.closeOptionsPage();
This would need to be invoked from within the options page itself.
Update
According to wOxxOm in the comments below, "window.close()" should work. So this may be a bug in my browser Edge.
wOxxOm suggested using "chrome.tabs.remove". However I get an error using it this way from within the options script:
chrome.tabs.getCurrent(function(tab) {
chrome.tabs.remove(tab.id, function() { });
});
Error handling response: TypeError: Cannot read property 'id' of undefined

Nightwatch-friendly command or javascript function to reload a page

I have been trying the various suggestions for reloading a page.
Reload a page manually
With Nightwatch magellan testarmada. The error I see is "TypeError: Cannot read property 'apply' of undefined"
Has anyone else run into this problem/ solved it?
EDIT: client.navigate().refresh(); does not work.
Embarrassingly obvious, once I fixed it. I was calling it in an external function but the problem amounted to this:
module.exports = new Test({
'#disabled': false,
"test page that needs a reload" : function(client){
client
// set url
.url(this.url)
.pause(this.timeout)
.navigate().refresh() // does not work
.refresh() // does work.
},
});
Refrain yourself from using navigate here. Use the mentioned code below:
return client.refresh();

CasperJS - using jQuery. ReferenceError: Can't find variable: jQuery/$

I'm writing code that involves jQuery in CasperJS. By chance, could someone point out the error I've made in including jQuery? (After 45 minutes of searching, I'm starting to think it's a local problem.)
I have tried both of the following:
casper.page.injectJs('C:\sweeps\jquery-1.10.2.min.js');
and
var casper = require('casper').create({
clientScripts: ["C:\sweeps\jquery-1.10.2.min.js"]
});
Code:
// sample.js
var casper = require('casper').create();
var login = "some username";
var password = "some password";
casper.start('https://www.paypal.com/us/home', function() {
this.fillXPath('form.login', {
'//input[#name="login_email"]': login,
'//input[#name="login_password"]': password,
}, true);
});
casper.page.injectJs('C:\sweeps\jquery-1.10.2.min.js');
$("input[name='submit.x']").click();
setTimeout(function(){
setTimeout(function(){
casper.run(function() {
this.captureSelector('example2.png', '#page');
this.echo('Done.').exit();
});
}, 30000); }, 1);
Output:
ReferenceError: Can't find cariable: jQuery
C:/sweeps/test2.js:21
The same result comes when "jQuery" is switched to "$".
EDIT: I've also tried relative pathing.
My reference is: Can I use jQuery with CasperJS?
Read this Casper#evaluate()
The concept behind this method is probably the most difficult to understand when discovering CasperJS. As a reminder, think of the evaluate() method as a gate between the CasperJS environment and the one of the page you have opened; everytime you pass a closure to evaluate(), you’re entering the page and execute code as if you were using the browser console.
casper.evaluate(function() {
$("input[name='submit.x']").click();
});
You need to use the jQuery selector as if you were in a browser.
Your path to the javascript file should be a URI relative to your HTML file, not a file-system path. Assuming your files are in the c:\sweepstakes folder, try
var casper = require('casper').create({
clientScripts: ["jquery-1.10.2.min.js"]
});
Also, use your browser's network/dev tools to see if your jQuery library is being downloaded or not.

iPad JavaScript Error not helpful

I get the following error and iPad but not in desktop browsers:
JavaScript: Error
undefined
TypeError: 'undefined' is not a function
This is a larger js application, and this error message is totally unhelpful. Is there any way I can get the line number of the error or anymore information?
Update: This just got funky.
line : 0
page : undefined
desc : TypeError: 'undefined' is not a function
chr : undefined
Did the user agent spoofing in FF and safari. No error.
You could try registering a custom error handler to window.onerror
window.onerror = function (desc,page,line,chr)
{ alert('Line:'+line); }
desc = Error message
page = File/Page where the error occured
line = Well...
chr = Character position of the error in the line
If you bind an error handler to window.onerror, it should give you the line number, e.g.
window.onerror = function(msg,url,line) {
alert('The error is on line '+line);
}
This question: Debug JavaScript errors on iPad seems to indicate you can enable debugging too.
If the script is loaded dynamically, though, it can be hard to get such info in any environment.

Problem using window.location in a function called from onsubmit

I'm currently creating a very small form on my homepage using HTML and JavaScript. I've run into a problem I'm pretty sure I could circumvent somehow (probably in a pretty ugly way though), but I got interested in why this error appears.
I have a form on my page which I specify in the following way:
<FORM name="form1" onsubmit="submitTheScript()">
The function "submitTheScript()" is placed in the header and reads:
setCookie("F_GivenSum", GivenSum);
window.location="LastScreen.html";
"setCookie()" is a function that basically just, well, creates a cookie :).
Now, the problem arises with the last line of code. I'm trying to send the user to another page, after the cookie has been set (I'm doing some controls that the input value is alright, but I've skipped that part here) but FireFox gives me the following error:
uncaught exception: [Exception... "Cannot modify properties of a WrappedNative" nsresult: "0x80570034 (NS_ERROR_XPC_CANT_MODIFY_PROP_ON_WN)" location: "JS frame :: chrome://global/content/bindings/autocomplete.xml :: onxblpopuphiding :: line 827" data: no]
I guess I can't call this function the way I do, from within a onsubmit command, however, I don't see why.
Okay, I did a quick test and found out that you need to assign the onsubmit event handler via javascript like so:
document.getElementById("myform").onsubmit = doSubmit;
function doSubmit() {
document.cookie = "F_GivenSum=200";
window.setTimeout(function() {
window.location = ('test2.html');
}, 20);
return false;
}
This did not give any errors in FF.

Categories

Resources