[ACCEPTED]-How to click a link element programmatially with HTMLElement?-webforms

Accepted answer
Score: 21

You have to find your element first, by 3 its ID or other filters:

HtmlElement fbLink = webBrowser.Document.GetElementByID("fbLink");

And to simulate 2 "click":

fbLink.InvokeMember("click");

An example for finding your link 1 by inner text:

HtmlElement FindLink(string innerText)
{
    foreach (HtmlElement link in webBrowser.Document.GetElementsByTagName("a"))
    {
        if (link.InnerText.Equals("Google Me"))
        {
            return link;
        }
    }
}
Score: 1

You need a way to automate the browser then.

One 10 way to do this is to use Watin (https://sourceforge.net/projects/watin/). It allows 9 you to write a .Net program that controls 8 the browser via a convenient object model. It 7 is mainly used to write automated tests 6 for web pages, but it can also be used to 5 control the browser.

If you don't want to 4 control the browser this way then you could 3 write a javascript that you include on your 2 page that does the clicking, but I doubt 1 that is what you are after.

More Related questions