admin管理员组

文章数量:1431912

<div class="test"> <span>
        <div>TEXT </div>
    </span>
    <div>
        <ul>
            <li> <span>Other text</span>
TEST1</li>
            <li>TEST2</li>
        </ul>
    </div>
</div>

How can I get all of the inner text of the div with test class ? Ideally I would like to have an array of string , something like : ["TEXT","Other text","TEST1","TEST2"].

<div class="test"> <span>
        <div>TEXT </div>
    </span>
    <div>
        <ul>
            <li> <span>Other text</span>
TEST1</li>
            <li>TEST2</li>
        </ul>
    </div>
</div>

How can I get all of the inner text of the div with test class ? Ideally I would like to have an array of string , something like : ["TEXT","Other text","TEST1","TEST2"].

Share Improve this question edited Jul 29, 2015 at 13:41 alecxe 475k127 gold badges1.1k silver badges1.2k bronze badges asked Jul 29, 2015 at 7:11 KarudiKarudi 2,8023 gold badges20 silver badges19 bronze badges 1
  • I researched it a bit more , I can actually get all of the text with this Xpath expression : ` //*[contains(concat(" ", normalize-space(@class), " "), "test")]/descendant::*/text() ` . The problem is when I use findElements , I get an error saying that the results are not WebElements. – Karudi Commented Jul 29, 2015 at 9:02
Add a ment  | 

2 Answers 2

Reset to default 3

You don't need to use the webdriver methods directly, protractor has a convenient abstraction around findElements() - element.all(). map() would help to get a promise resolving into an array of texts:

var texts = element.all(by.xpath("//div[@class='test']//*")).map(function (elm) {
    return elm.getText();
});

expect(texts).toEqual(["TEXT", "Other text", "TEST1", "TEST2"]);

try something like this:

 List<WebElement> list = driver.findElements(By.xpath("//div[@class='test']"));
 for(WebElement element: list){
    //print text if text not empty
    String text = element.getText();
    if(!text.isEmpty){
      S.O.P("Result :"+text);
    }
 }

本文标签: javascriptSelenium get all inner text of an element with ProtractorStack Overflow