How can I get text node without child?

9,748 views
Skip to first unread message

Anatoli Kuzmin

unread,
Nov 26, 2009, 9:11:53 AM11/26/09
to webdriver
Hi

I have a problem - I cannot get text for element without child text nodes

Html example:
<div>Text
        <div>Another Text</div>
</div>

I found element and called getText()
result: Text Another Text
Driver: Firefox


--
Best regards,
Anatoli Kuzmin

lenocin

unread,
Nov 30, 2009, 10:16:18 AM11/30/09
to webdriver
You found the outer <div>.
Are you using xpath? If so just add //div

John Lockhart

unread,
Nov 30, 2009, 2:17:04 PM11/30/09
to webd...@googlegroups.com
I think he wants the outer div content without the inner i.e. just to get "Text" returned.

I must admit I haven't come across this problem and was surprised to find it doesn't work for me either (using FitLibraryWeb).

I tried //div/div/.. in case it was because //div was looking at a node-set containing all the divs, but instead it looks like getText is simply returning all the text content of that element and the sub-elements.

So I'm curious to see the answer to this, and somewhat surprised it hasn't come up as a problem for me before.

cheers,
       John (john at webtest.co.nz / www.webtest.co.nz)



--

You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.



Marc Guillemot

unread,
Nov 30, 2009, 2:47:29 PM11/30/09
to webd...@googlegroups.com
Hi,

I think that it is not possible with the current WebDriver API that can
only return WebElement(s) for a finder. In fact it is a pity because
XPath is really powerful and can be used to access text nodes of course
but as well the result of some aggregation (count(...)) for instance.

Cheers,
Marc.

John Lockhart a �crit :
> I think he wants the outer div content without the inner i.e. just to
> get "Text" returned.
>
> I must admit I haven't come across this problem and was surprised to
> find it doesn't work for me either (using FitLibraryWeb).
>
> I tried //div/div/.. in case it was because //div was looking at a
> node-set containing all the divs, but instead it looks like getText is
> simply returning all the text content of that element and the sub-elements.
>
> So I'm curious to see the answer to this, and somewhat surprised it
> hasn't come up as a problem for me before.
>
> cheers,
> John (john at webtest.co.nz <http://webtest.co.nz> /
> www.webtest.co.nz <http://www.webtest.co.nz>)
>
>
> On Tue, Dec 1, 2009 at 4:16 AM, lenocin <len...@gmail.com
> <mailto:len...@gmail.com>> wrote:
>
> You found the outer <div>.
> Are you using xpath? If so just add //div
>
> On Nov 26, 10:11 pm, Anatoli Kuzmin <anatoli.kuz...@gmail.com
> <mailto:anatoli.kuz...@gmail.com>> wrote:
> > Hi
> >
> > I have a problem - I cannot get text for element without child
> text nodes
> >
> > Html example:
> > <div>Text
> > <div>Another Text</div>
> > </div>
> >
> > I found element and called getText()
> > result: Text Another Text
> > Driver: Firefox
> >
> > --
> > Best regards,
> > Anatoli Kuzmin
>
> --
>
> You received this message because you are subscribed to the Google
> Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com
> <mailto:webd...@googlegroups.com>.
> To unsubscribe from this group, send email to
> webdriver+...@googlegroups.com
> <mailto:webdriver%2Bunsu...@googlegroups.com>.

Mirko Friedenhagen

unread,
Nov 30, 2009, 4:08:02 PM11/30/09
to webd...@googlegroups.com
Am 30.11.2009 um 20:47 schrieb Marc Guillemot:
> Hi,
>
> I think that it is not possible with the current WebDriver API that can
> only return WebElement(s) for a finder. In fact it is a pity because
> XPath is really powerful and can be used to access text nodes of course
> but as well the result of some aggregation (count(...)) for instance.

I had a slightly different problem with something like this:
<caption>
<span>Just a test</span>
<input type="button".../>
Foobar my heart
</caption>

where I wanted to get any element with text containing "Foobar".

I first tried sth. like this:
//*[contains(text(), "Foobar")]

However this did not find the caption.
Now I have
//*/text()/[contains(., "Foobar")]
and this does find "Foobar"

So maybe //div/text() will work?

Regards
Mirko

> Cheers,
> Marc.
>
> John Lockhart a écrit :

Alex Trofimov

unread,
Jan 8, 2015, 1:08:02 PM1/8/15
to webd...@googlegroups.com
I ended up getting the text and removing any text from the children:

public static String geNodeText(WebElement element) {
    String text = element.getText();
    for (WebElement child : element.findElements(By.xpath("./*"))) {
      text = text.replaceFirst(child.getText(), "");
    }
    return text;
  }

This should work in most cases.

Selenium Framework

unread,
Jan 8, 2015, 4:49:32 PM1/8/15
to webd...@googlegroups.com
That is the way selenium/watir works I guess - returns the text of current and all child elements text -- Read this - https://leanpub.com/watirways/read#leanpub-auto-text-1

Virgil Suteu

unread,
Sep 3, 2015, 11:02:58 AM9/3/15
to webdriver
Thank you very much Alex! Simple and classy fix, much appreciated.

Bhuvan Bhaskar Dev

unread,
Dec 3, 2016, 7:27:30 AM12/3/16
to webdriver
Hi Alex, 

How do i remove the the text of children inside children
Reply all
Reply to author
Forward
0 new messages