Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WatiN: When finding text, how do I get a reference to its containing element?

Tags:

c#

watin

Given the following HTML page:

<html>
<head>
    <title>WatiN Test</title>
</head>
<body>
<div>
    <p>Hello World!</p>
</div>
</body>
</html>

I would like to be able to search for some text (lets say "World"), and get a reference to its parent element (in this case the <p> element).


If I try this:

var element = ie.Element(Find.ByText(t => t.Contains("World")))

or this:

var element = ie.Element(e => e.Text != null && e.Text.Contains("World"));

I get back the <html> element. This is consistent with the WatiN documentation for Element.Text which states: "Gets the innertext of this element (and the innertext of all the elements contained in this element)".

Since all text within the page is contained within the <html> element, I'll always get this back instead of the immediate parent.


Is there a way to get just the text immediately beneath an element (not the text within the elements contained by it)?

Is there another way of doing this?

Many thanks.

like image 305
James Morcom Avatar asked Jan 19 '11 14:01

James Morcom


2 Answers

You could use Find.BySelector

var element = ie.Element(Find.BySelector("p:contains('World')"));
like image 183
qlayer Avatar answered Oct 10 '22 03:10

qlayer


Bellow code will find first element inside the <body/> with text containing "World". Elements which are classified as element containers and has child elements will be omitted.

var element = ie.ElementOfType<Body>(Find.First()).Element(e =>
{
    if (e.Text != null && e.Text.Contains("World"))
    {
        var container = e as IElementContainer;
        if (container == null || container.Elements.Count == 0)
            return true;
    }
    return false;
});

Note: You may wonder why I wrote ie.ElementOfType<Body>(Find.First()).Element instead of just ie.Element. This should work, but it doesn't. I think it's a bug. I wrote a post about it on WatiN mailing list and will update this answer when I receive the answer.

like image 40
prostynick Avatar answered Oct 10 '22 02:10

prostynick