javascript 如何正确使用 getElementByXpath 和 getElementsByXpath?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34239161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use getElementByXpath and getElementsByXpath correctly?
提问by Erik Braune
How can I get table 'td' values with CasperJS?
如何使用 CasperJS 获取表“td”值?
The HTML source looks like than this:
HTML 源代码如下所示:
<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>
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?
我想使用 CasperJS 获取表值。首先,我需要选择表格的行;然后我想获得 '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.
我尝试了很多方法,但都没有奏效。我的解决方案看起来类似于您可以在下面看到的内容。重要的是,首先选择“table_rows”;然后在 for 循环中选择那个 td 值。
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;
}
采纳答案by Artjom B.
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()
.
CasperJS 有两个上下文。您只能直接从您可以访问1内部的页面上下文访问 DOM 。它是沙盒的,因此在外部定义的变量在.casper.evaluate()
evaluate()
__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.
__utils__.getElementsByXpath()
并且__utils__.getElementByXpath()
仅在不可用的页面上下文中casper
可用。这两个函数直接返回DOM节点,所以这些节点本身没有这个getElementByXpath()
函数。
But you don't need that at all:
但你根本不需要它:
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));
});
You can use all of the ways to traverse the DOM like children
, querySelector()
or document.evaluate()
.
您可以使用所有方法来遍历 DOM,例如children
、querySelector()
或document.evaluate()
。
1Please also read the PhantomJS documentation of the same function.
1另请阅读相同功能的PhantomJS 文档。