gpt4 book ai didi

javascript - 从 XPath 打印元素会产生更多的元素

转载 作者:行者123 更新时间:2023-12-03 11:41:17 25 4
gpt4 key购买 nike

我想做的就是从我确定的 xpath 返回一个字符串,但我在使用 CasperJS 中的 getElementsByXPath 函数时遇到问题。

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":

等等。

最佳答案

您无法从 CasperJS 中的页面上下文返回 DOM 元素。如果使用 document.querySelectorAll 而不是 __utils__.getElementsByXPath(使用调整后的 CSS 选择器),结果将是一个 undefined 值的数组。 __utils__.getElementsByXPath 的情况并非如此。返回的 DOM 元素的快照实际上是部分可序列化的。问题是它们包含对文档的循环引用,并且会不断增长。

来自docs :

Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.

Closures, functions, DOM nodes, etc. will not work!

您需要在页面上下文中执行您想要执行的所有操作。例如作为字符串的元素:

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

关于javascript - 从 XPath 打印元素会产生更多的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26259789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com