2

I would like to know how to retrieve an "a" element into a div.

I have an HTML like :

<div id="content">
<div class="divSearchForm">
    <table>
        <tbody>
            <tr id="tr1">
                <td>
                    <img class="SearchImage">
                        List of systems
                </td>
                <td>
                    <table width="100%" class="Result">
                        <tbody>
                            <tr>
                                <td>
                                    <a href="test">K</a>
                                </td>
                            </tr>                            
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
</div>

And I would like to know how to get the "K" link.

For now, I have :

webBrowser1.Document.GetElementById("content").GetElementsByTagName("a");

But it looks like nothings happened.

Do you have any idea? Thanks

4
  • 2
    add runas="server' and an id to the hyperlink and you can access the hyperlink normally Commented Feb 2, 2015 at 13:30
  • If you are trying to parse HTML I would recomend you using github.com/jamietre/CsQuery Commented Feb 2, 2015 at 13:30
  • Are you sure the document is already completely loaded when you are calling GetElementById("content") ? Commented Feb 2, 2015 at 13:39
  • @JonathanCamilleri, I cannot add an id to the hyperlink. I'm not the creator of the Website, i'm just using it Commented Feb 2, 2015 at 13:39

1 Answer 1

3

Most likely you are accessing the Document before it is completely loaded.

Add a listener to the DocumentCompleted event of the WebBrowser control

webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;

and try to access your Elements there:

protected void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    HtmlElement el = webBrowser1.Document.GetElementById("content").GetElementsByTagName("a")[0];
    String anchorText = el.InnerText; // will contain "K"
    String url = el.GetAttribute("href"); // will contain "test"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.