Retrieving all rows of a table and returning them using an XPath query in CasperJS

I use Casper.js to automate regular downloads. I managed to download the file and check if it really is, but I would like to analyze the table that is returned if there are errors, but I get the error [error] [remote] findAll(): invalid selector provided "[object Object]":Error: SYNTAX_ERR: DOM Exception 12 . Here is the relevant part of my code:

 casper.then(function() { if (this.fetchText('.statusMessageContainer').match(/Sorry, the file did not pass validation. Please review the validation errors in the report below/)) { this.echo("Upload failed!", "ERROR"); errors = this.evaluate(function() { var errorRows = __utils__.findAll({ type: 'xpath', path: '//table[@id="uploadTable"]/tr[position()>1]' }); return Array.prototype.forEach.call(errorRows, function(e) { return e; }); }); this.echo(JSON.stringify(errors)); } else { this.echo("Upload successful", "INFO"); } }); 

Any ideas?

+3
source share
2 answers

Although you probably have an XPath syntax error, you should be aware that you cannot return DOM elements from the closure passed to the evaluate() method; you need to convert the NodeList and HTMLelement to some native Javascript types, for example. Arrays, objects, strings, etc.

In addition, there is a convenient method getElementsByXPath() in the ClientUtils module , which you can use from the __utils__ instance, which is automatically entered on each page, load:

 casper.then(function() { if (this.fetchText('.statusMessageContainer').match(/Sorry, the file did not pass validation. Please review the validation errors in the report below/)) { this.echo("Upload failed!", "ERROR"); var errors = this.evaluate(function() { var errorRows = __utils__.getElementsByXPath('//table[@id="uploadTable"]/tr[position()>1]'); return Array.prototype.map.call(errorRows, function(e) { return e.innerText; // let get node text instead of HTMLelement! }); }); this.echo(JSON.stringify(errors)); } else { this.echo("Upload successful", "INFO"); } }); 

You can also use the ClientUtils Client Booklet to test your selectors directly in your browser console. For example, here click the bookmarklet and do this in the js console:

 __utils__.getElementsByXPath('//table[@id="uploadTable"]/tr[position()>1]') 

Then you will see if your selector is correct (it works on my side - I mean that it is syntactically correct).

+2
source

Well, due to your mistake, there seems to be something wrong with your selector. It is correctly configured from what I see, except for one thing: try changing '//table[@id="uploadTable"]/tr[position()>1]' to '//table[@id='uploadTable']/tr[position()>1]' (change "to")

Also, your XPath looks syntactically correct, so I'm not sure why it will qualify as an invalid selector.

0
source

All Articles