最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to get innerHTML using CasperJS? - Stack Overflow

matteradmin4PV0评论

I want to get attribute of only string in <em> tags of HTML page

I want to get "(868)"

1.

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

2.

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

I tried both but it returns "null" How to solve the problem?

I want to get attribute of only string in <em> tags of HTML page

I want to get "(868)"

1.

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

2.

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

I tried both but it returns "null" How to solve the problem?

Share Improve this question edited Sep 14, 2015 at 19:29 Artjom B. 62k26 gold badges135 silver badges230 bronze badges asked Sep 14, 2015 at 18:44 Hyung Kyu ParkHyung Kyu Park 2652 gold badges3 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

<em> is not an element attribute. It's an element itself. casper.getElementAttribute(selector, attribute) will correctly retrieve the attribute text of an element, but your want to get the element text.

You can use casper.fetchText(selector) for that. Note that fetchText() will concatenate the contents of all matched elements into one string. If you don't want that, you either need to make sure that the selector only matches a single element or use other functions such as casper.getElementInfo(selector).text.


Your second snippet cannot work, because you forgot " around the selector and because of the above reason.

Take a look at documentation FAQ Can I access & manipulate DOM elements directly from the CasperJS environment?.

In the both of examples that you added in your question you tried to get the em element as an attribute of h4 and that wrong because em is a child and not attribute of h4 tag, so to select textContent of an element you can try to use querySelector with evaluate function like following :

casper.then(function() {
    var text = this.evaluate(function(){
        return document.querySelector("h4.head em").textContent;
    });

    var word = require('utils').dump(text);
    console.log(word);
}

Hope this helps.

Post a comment

comment list (0)

  1. No comments so far