2014-10-08 78 views
0

我想要做的就是從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": 

等。

+1

在不相關的註釋上:XPath表達式非常嚴格。爲什麼不使用'// div [@ id ='tabs1']/figure [@ class ='youTube']/h4/a'? – 2014-10-08 15:36:35

+0

我增加了一個更完整的例子。我也改變了它,所以它不太僵化。沒有理由這麼僵硬。我還在學習XPath如何工作。 – Matt 2014-10-08 15:42:44

回答

1

您不能從CasperJS的頁面上下文中返回DOM元素。如果使用document.querySelectorAll而不是__utils__.getElementsByXPath(帶有經過調整的CSS選擇器),則結果將是一個undefined值的數組。 __utils__.getElementsByXPath的情況並非如此。返回的DOM元素快照實際上是部分可序列化的。問題在於它們包含對文檔的循環引用,並且會不斷增長。

docs

注:參數和返回值的評估函數必須是一個簡單的原始對象。經驗法則:如果它可以通過JSON序列化,那麼它很好。

閉包,函數,DOM節點等不起作用!

你需要在頁面上下文中做所有你想做的事情。例如,元素作爲字符串:

content = casper.evaluate(function() { 
    return __utils__.getElementByXPath(someSelector).outerHTML; 
}); 
+0

由於某種原因返回null – Matt 2014-10-08 16:33:53

+1

請注意'getElementByXPath'中缺少's'。如果您使用'getElementsByXPath',則必須映射元素或遍歷它們。 – 2014-10-08 16:40:32

+0

就是這樣。非常感謝! – Matt 2014-10-08 16:41:24