CasperJS按类获取元素的innerHTML [英] CasperJS getting innerHTML of element by class
问题描述
我是CasperJS的新手,我在使用< p class =city> Data I Need< / p>
I'm new to CasperJS and I'm having a few issues getting the innerHTML out of <p class="city">Data I Need</p>
我尝试了一些东西,但似乎没有任何东西可以得到它。
I have tried a few things, but nothing seems to get it.
var city_name= casper.evaluate(".//*[@class='city_name']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
var friend_username = city_name.innerHTML;
和
var city_name = this.evaluate(function() {
return document.querySelector(".//*[@class='city_name']").innerHtml;
});
推荐答案
CasperJS的工作原理CSS选择器的默认值:
CasperJS works by default with CSS selectors:
var city_name = casper.evaluate(function() {
return document.querySelector(".city_name").innerHTML;
});
请注意,该属性为 innerHTML
不的innerHTML
。另请注意, casper.evaluate()
是页面上下文的接口,与 document.evaluate()$ c $无关c>。
Note that the property is innerHTML
not innerHtml
. Also note that casper.evaluate()
is the interface to the page context and has nothing to do with document.evaluate()
.
您当然可以将CasathJS实用程序与XPath表达式一起使用。像 casper.getElementInfo()$ c这样的功能$ c>
为您提供一些额外的属性,如 html
,这实际上是相应的 innerHTML
属性DOM元素。
You can of course use XPath expressions with a CasperJS utility. Functions like casper.getElementInfo()
provide you with some additional properties like html
which is actually the innerHTML
property on the corresponding DOM element.
var x = require("casper").selectXPath;
...
var city_name = casper.getElementInfo(x(".//*[@class='city_name']")).html;
这篇关于CasperJS按类获取元素的innerHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!