0

I'm trying to teach myself C# the program I'm working on has a browser attached to it. I have been trying to figure out how to make it click a button by the tag name but I cant seem to get it to. I have searched this site and found many helpful topics but none yet to help solve this one.

<a class="some_class" style="letter-spacing: -1px" href="/someurl" data-executing="0">displaybuttontext</a>

That is my button I would like to make the program click. But cant get it to. Here are a few ways I have tried:

        private void test1_CheckedChanged_1(object sender, EventArgs e)
    {
        web.Document.GetElementsByTagName("class").GetAttribute("href").invokemember("click");
    }    

That method didn't it threw up a error with GetAttribute, I then tried:

 moco.Document.GetElementById("a").InvokeMember("click");    

This doesn't show an error but also doesn't click the button (displaybuttontext). Could someone be kind enough to show a example and explain it. Keep in mind that I'm new to this.

1 Answers1

1

Let's say you have this on the html page:

<a id="link-id" href="linktosomewhere.html">click here</a>

You would want to call:

Document.GetElementById("link-id").InvokeMember("click");  

This is because the link element has the attribute id="link-id".

You can see in this article that the parameter you pass to GetElementById needs to be the ID of the element you want to retrieve: http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementbyid(v=vs.110).aspx

lloyd christmas
  • 556
  • 1
  • 4
  • 16