获取所有表行并使用CasperJS中的XPath查询返回它们 [英] Getting all table rows and returning them using an XPath query in CasperJS

查看:77
本文介绍了获取所有表行并使用CasperJS中的XPath查询返回它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Casper.js自动定期上传。我已设法上传文件并检查它是否有效,但我想解析如果有错误则返回的表,但是我得到错误 [error] [remote] findAll() :无效选择器提供[object Object]:错误:SYNTAX_ERR:DOM Exception 12 。这是我的代码的相关部分:

I'm using Casper.js to automate a regular upload. I've managed to upload the file and check if it's valid, but I'd like to parse the table which is returned if there's errors, but I get the error [error] [remote] findAll(): invalid selector provided "[object Object]":Error: SYNTAX_ERR: DOM Exception 12. Here's 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");
    }
});

任何想法?

推荐答案

虽然您可能有XPath语法错误,但您必须知道不能从传递给 evaluate()方法的闭包中返回DOM元素。您必须将 NodeList HTMLelement 实例转换为某些本机Javascript类型,例如。数组,对象,字符串等...

While you probably have an XPath syntax error, you must know that you cannot return DOM elements from a closure passed to the evaluate() method; you have to convert your NodeList and HTMLelement instances to some native Javascript types, eg. Arrays, Objects, strings, etc…

此外,还有一个方便的您可以在每个页面中自动注入的 __ utils __ 实例中使用 getElementsByXPath() ClientUtils模块中的方法你的负载:

Also, there's a convenient getElementsByXPath() method in the ClientUtils module you can use from the __utils__ instance automatically injected in every page your 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's get node text instead of HTMLelement!
            });
        });
        this.echo(JSON.stringify(errors));
    } else {
        this.echo("Upload successful", "INFO");
    }
});

您还可以使用 ClientUtils bookmarklet 也可以在浏览器控制台中测试您的选择器。例如,单击bookmarklet并在js控制台中执行:

You can also use the ClientUtils bookmarklet to test your selectors right within your browser console as well. For example here, click the bookmarklet and execute this in the js console:

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

然后你会看看你的选择器是否正确(它在我身边起作用 - 我的意思是它在语法上是正确的。)

Then you'll see if your selector is correct (it works by my side — I mean it is syntactically correct).

这篇关于获取所有表行并使用CasperJS中的XPath查询返回它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆