0

I have a web page that has the following html code:

....
<div class="some_class">Text I want 
   <span class="another_class">Text I don't want</span>
   ....more junk...
   ....a lot more junk....
</div>

I tried to call getText(), which gives me all the text within the div which contains a lot of other text that I don't want. My question is: how can I get ONLY the text I want without resort to some kind of parsing or extracting substring etc?

Thank you!

4
  • Not hat I can think of, but I can help you figure an acceptably reliable way to do it with parsing/substringing if you change your mind. Sep 6, 2013 at 20:38
  • I am doing it with substring for now: finding the index of the text by class name "another_class" and sub-string 0 to that index. Sep 7, 2013 at 14:30
  • And is that working for you? Sep 7, 2013 at 17:23
  • @user1596683 have you tried my solution? It should work well for you.
    – d0x
    Sep 9, 2013 at 21:30

1 Answer 1

0

You need to do this with JavaScript. If your webDriver (like Firefox, Chrome, ...) supports that, you can do it like this:

public class Main
{

    //@formatter:off
    final static String JS_SCRIPT_GET_TEXT  = "var element = arguments[0];                                      " +
                                              "var text = '';                                                   " + 
                                              "for (var i = 0; i < element.childNodes.length; i++)              " + 
                                              " if (element.childNodes[i].nodeType === Node.TEXT_NODE)          " + 
                                              " {                                                               " + 
                                              "   text += element.childNodes[i].textContent + ' ';              " +
                                              " }                                                               " + 
                                              "return text;                                                     ";
    //@formatter:on

    public static void main(final String[] args)
    {
        final FirefoxDriver driver = new FirefoxDriver();

        driver.get("http://en.wikipedia.org/wiki/HTML");

        final List<WebElement> findElementsByCssSelector = driver.findElementsByCssSelector("#mw-content-text div");

        final WebElement webElement = findElementsByCssSelector.get(0);

        final String extractInnerText = extractInnerText(webElement, driver);

        System.out.println("---------------------");
        System.out.println("Seleniums .getText():\n" + webElement.getText());

        System.out.println("\n\n---------------------");
        System.out.println("Just the node text:\n" + extractInnerText);

    }

    public static String extractInnerText(final WebElement webElement, final WebDriver webDriver)
    {
        final JavascriptExecutor javascriptExecutor = (JavascriptExecutor) webDriver;

        String webElementText = (String) javascriptExecutor.executeScript(JS_SCRIPT_GET_TEXT, webElement);
        webElementText = webElementText.trim();

        return webElementText;
    }
}

For this example Html:

<div class="dablink">
    For the use of HTML on Wikipedia, see
    <a href="/wiki/Help:HTML_in_wikitext" title="Help:HTML in wikitext">Help:HTML in wikitext</a>
    .
</div>

It will print you:

---------------------
Seleniums .getText():
For the use of HTML on Wikipedia, see Help:HTML in wikitext.


---------------------
Just the node text:
For the use of HTML on Wikipedia, see  .

I think thats what you need. You can apply the extractInnerText(..) method to every webelement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.