如何使用CasperJS获取innerHTML?

Hyu*_*ark 4 javascript innerhtml casperjs

我想在<em>HTML页面的标签中获取仅字符串的属性

在此输入图像描述

我想得到"(868)"

1.

casper.then(function() {
     var word = require('utils').dump(this.getElementAttribute(x('//*[@id="content"]/div[2]/h4/em'), 'em'));
     console.log(word)
});
Run Code Online (Sandbox Code Playgroud)

2.

casper.then(function() {
    var word = require('utils').dump(this.getElementAttribute(h4[class="head"], 'em'));
    console.log(word)
});
Run Code Online (Sandbox Code Playgroud)

我试过两个但它返回"null"如何解决问题?

Art*_* B. 7

<em>不是元素属性.它本身就是一个元素.casper.getElementAttribute(selector, attribute)将正确检索元素的属性文本,但您想获取元素文本.

你可以用casper.fetchText(selector)它.请注意,fetchText()将所有匹配元素的内容连接成一个字符串.如果您不希望这样,您需要确保选择器仅匹配单个元素或使用其他功能,例如casper.getElementInfo(selector).text.


你的第二个片段无法正常工作,因为你忘记"了选择器并且由于上述原因.