如何正确使用getElementByXpath和getElementsByXpath? [英] How to use getElementByXpath and getElementsByXpath correctly?

查看:3138
本文介绍了如何正确使用getElementByXpath和getElementsByXpath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用CasperJS获取表'td'的值?

How can I get table 'td' values with CasperJS?

HTML源代码如下:

The HTML source looks like than this:

<table id="my_table">
  <tr id='header'>
    <th>sth_head_name</th>
    <th>ath_head_name</th>
    <th>sth_head_name</th>
    <th>sth_head_name</th>
    <th>sth_head_name</th>
  </tr>
  <tr>
    <td>sth_value</td>
    <td>sth_value</td>
    <td>sth_value</td>
    <td>sth_value</td>
    <td>sth_value</td>
  </tr>
  <tr>
    <td>sth_value</td>
    <td>sth_value</td>
    <td>sth_value</td>
    <td>sth_value</td>
    <td>sth_value</td>
  </tr>
  <tr>
    <td>sth_value</td>
    <td>sth_value</td>
    <td>sth_value</td>
    <td>sth_value</td>
    <td>sth_value</td>
  </tr>
</table>

我想使用CasperJS获取表值。首先,我需要选择表格的行;然后我想获得 td值。我该如何解决呢?

I'd want to get table values using CasperJS. Firstly, I need to select the rows of table; and then I want to get 'td' values. How can I solve this?

我尝试了很多方法,但是这些方法没有用。我的解决方案看起来类似,您可以在下面看到。重要的是,首先选择 table_rows;然后在for循环中选择td值。

I tried a lot of ways, but those didn't work. My solution would look like something similar that you can see below. Its important, that firstly select 'table_rows'; and then select that's td value inside the for cycle.

var table_rows = casper.getElementsByXpath("//table[@id='my_table']/tr[not(@id='header')]");

for (var i = 0; i < table_rows.length; i++) {
  var firstRequiredCell_query = table_rows[j].getElementByXpath("//td[position()=2]");
  var secondRequiredCell_query = table_rows[j].getElementByXpath("//td[position()=4]");
  var firstRequiredCell = firstRequiredCell_query.text;
  var secondRequiredCell = secondRequiredCell_query.text;
}


推荐答案

CasperJS有两个上下文。您只能直接从 casper.evaluate() 1 。它是沙箱,因此外部定义的变量在 evaluate()中不可用。

CasperJS has two contexts. You can only access the DOM directly only from the page context which you get access to inside of casper.evaluate()1. It is sandboxed and therefore variables defined outside are not available in evaluate().

__ utils __。getElementsByXpath() __ utils __。getElementByXpath()仅在 casper 不可用。这两个函数直接返回DOM节点,因此这些节点本身没有 getElementByXpath()函数。

__utils__.getElementsByXpath() and __utils__.getElementByXpath() are only available in the page context where casper is not available. Those two functions return DOM nodes directly, so those nodes itself don't have the getElementByXpath() function on them.

但是您根本不需要:

casper.then(function(){
    var info = this.evaluate(function(){
        var table_rows = __utils__.getElementsByXpath("//table[@id='my_table']/tr[not(@id='header')]");

        return table_rows.map(function(tr){
            return {
                a: tr.children[1].textContent,
                b: tr.children[3].textContent
            };
        });
    });
    this.echo(JSON.stringify(info, undefined, 4));
});

您可以使用所有方式遍历DOM,例如 children querySelector() document.evaluate()

You can use all of the ways to traverse the DOM like children, querySelector() or document.evaluate().

1 也请阅读 PhantomJS文档

这篇关于如何正确使用getElementByXpath和getElementsByXpath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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