我想要做的就是从xpath返回一个字符串,但我在CasperJS中使用getElementsByXPath
函数时遇到了问题。从XPath打印元素的结果超过了元素
var casper = require('casper').create({
verbose: false,
logLevel: 'debug'
});
casper.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36");
casper.start();
casper.thenOpen('http://www.uky.edu', function(){
content = casper.evaluate(function() {
return __utils__.getElementsByXPath('//div[@id=\'container\']/section[@id=\'content\']/aside[@class=\'socialTab clearfix\']/div[@id=\'usual2\']/div[@id=\'tabs1\']/figure[@class=\'youTube\']/h4/a');
});
});
casper.run(function() {
this.echo(JSON.stringify(content));
this.echo('completed').exit();
});
不停止返回数据。它绝对不会返回我正在寻找的特定字符串,而是它将返回整个网页的数据。
我结束了一个几乎空白的网页,只用一个DIV想这一点,得到了同样的问题 我用// DIV的XPath和收到以下
[{"align":"","attributes":{"length":0},"baseURI":"http://download2012.ad.uky.edu/caspertest.php","childElementCount":0,"childNodes":{"0":null,"length":1},"child
ren":{"length":0},"classList":{"length":0},"className":"","clientHeight":20,"cli
entLeft":0,"clientTop":0,"clientWidth":384,"contentEditable":"inherit","dataset"
:{},"dir":"","draggable":false,"firstChild":null,"firstElementChild":"","hidden"
:false,"id":"","innerHTML":"Test2","innerText":"Test2","isContentEditable":false
,"lang":"","lastChild":{"attributes":"","baseURI":"http://download2012.ad.uky.ed
u/caspertest.php","childNodes":{"length":0},"data":"Test2","firstChild":"","last
Child":"","length":5,"localName":"","namespaceURI":"","nextSibling":"","nodeName
":"#text","nodeType":3,"nodeValue":"Test2","ownerDocument":null,"parentElement":
null,"parentNode":null,"prefix":"","previousSibling":"","textContent":"Test2","w
holeText":"Test2"},"lastElementChild":"","localName":"div","namespaceURI":"http:
//www.w3.org/1999/xhtml","nextElementSibling":"","nextSibling":null,"nodeName":"
DIV","nodeType":1,"nodeValue":"","offsetHeight":20,"offsetLeft":8,"offsetParent"
:{"aLink":"","attributes":{"length":0},"background":"","baseURI":"http://downloa
d2012.ad.uky.edu/caspertest.php","bgColor":"","childElementCount":1,"childNodes"
:{"0":null,"1":null,"2":null,"length":3},"children":{"0":null,"length":1},"class
List":{"length":0},"className":"","clientHeight":300,"clientLeft":0,"clientTop":
0,"clientWidth":400,"contentEditable":"inherit","dataset":{},"dir":"","draggable
":false,"firstChild":{"attributes":"","baseURI":"http://download2012.ad.uky.edu/
caspertest.php","childNodes":{"length":0},"data":"Test\n","firstChild":"","lastC
hild":"","length":5,"localName":"","namespaceURI":"","nextSibling":null,"nodeNam
e":
等。
在不相关的注释上:XPath表达式非常严格。为什么不使用'// div [@ id ='tabs1']/figure [@ class ='youTube']/h4/a'? – 2014-10-08 15:36:35
我增加了一个更完整的例子。我也改变了它,所以它不太僵化。没有理由这么僵硬。我还在学习XPath如何工作。 – Matt 2014-10-08 15:42:44