简体   繁体   English

检索HTML层次结构中的元素

[英]Retrieve element within html hierarchy

I have this piece of html code. 我有这段html代码。 I want to get the text inside the <div> tag using WatiN. 我想使用WatiN在<div>标记内获取文本。 The C# code is below, but I'm pretty sure it could be done way better than my solution. 下面是C#代码,但我可以肯定它可以比我的解决方案更好。 Any suggestions? 有什么建议么?

HTML: HTML:

<table id="someId" cellspacing="0" border="1" style="border-collapse:collapse;" rules="all">
    <tbody>
        <tr>
            <th scope="col">&nbsp;</th>
        </tr>
        <tr>
            <td>
                <div>Some text</div>
            </td>
        </tr>
    </tbody>
</table>

C# C#

// Get the table ElementContainer
IElementContainer diagnosisElementContainer = (IElementContainer)_control.GetElementById("someId");

// Get the tbody element
IElementContainer tbodyElementContainer = (IElementContainer)diagnosisElementContainer.ChildrenWithTag("tbody");

// Get the <tr> children
ElementCollection trElementContainer = tbodyElementContainer.ChildrenWithTag("tr");

// Get the <td> child of the last <tr>
IElementContainer tdElementContainer = (IElementContainer)trElementContainer.ElementAt<Element>(trElementContainer.Count - 1);

// Get the <div> element inside the <td>
Element divElement = tdElementContainer.Divs[0];

Based on the given, something like this is how I'd go for IE. 根据给定的内容,这就是我要使用IE的方式。

IE myIE = new IE();
myIE.GoTo("[theurl]");
string theText = myIE.Table("someId").Divs[0].Text;

The above is working on WatiN 2.1, Win7, IE9. 以上内容适用于WatiN 2.1,Win7,IE9。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM