4

I searched the internet and found examples for;

"How to click a button which is in a webBrowser(internet explorer) in C# ?"

And here is the code which is working on google;

JS :

    void ClickButton(string attribute, string attName)
    {
        HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("input");
        foreach (HtmlElement element in col)
        {
            if (element.GetAttribute(attribute).Equals(attName))
            {
                element.InvokeMember("click");   // Invoke the "Click" member of the button
            }
        }
    }

But my webpage button has different tags. So the program can't detect to click it.

My main question is; How to click this button programmatically ?

HTML :

<a class="orderContinue" href="Addresses" title="Sipar Ver">Sipar Devam</a>
Joffrey Maheo
  • 2,919
  • 2
  • 20
  • 23
Lost_In_Library
  • 3,265
  • 6
  • 38
  • 70
  • What do you want clicking the link to achieve? To go to the address in the href? – Jon Jan 24 '12 at 13:58
  • Do you need this for testing? or this is part of your program? Also, what do you mean by "has different tags". – Reza Jan 24 '12 at 13:59

2 Answers2

8

Naturally the code you've posted won't find the tag you've posted. It's looking for tags of type input:

webBrowser1.Document.GetElementsByTagName("input")

But as you say (and demonstrate):

But my webpage button has different tags.

Thus, you need to look for the tag(s) that you use. Something like this:

webBrowser1.Document.GetElementsByTagName("a")

This would return the anchor elements within the document. Then, naturally, you need to find the specific one(s) you want to click. That's what this line is doing:

if (element.GetAttribute(attribute).Equals(attName))

Whether it finds the target tag(s) depends entirely on the values for those variables, which I assume you know and can manage.

David
  • 208,112
  • 36
  • 198
  • 279
  • Thank you very much for the answer. But I don't understand it. webBrowser1.Document.GetElementsByTagName("a") can collect all href-s. And then? – Lost_In_Library Jan 24 '12 at 14:22
  • @LostInLib: And then what? The code you posted is collecting the `input` elements in the document loaded into a `WebBrowser` object. It then loops through them, looking for a specific attribute with a specific value (which may match zero or more elements) and invokes a click on matched elements. All you need to do to switch from `input` elements to `a` elements is change the argument to the `GetElementsByTagName` method. What more is needed? In what way is it not working for you? – David Jan 24 '12 at 14:27
-1

Using jQuery, you could put a click event on that a tag.
You would want to place this code at the bottom of your page

<script>
$(document).ready(function() { 
    $('.orderContinue').click(function() {
       alert('Sipar Ver has been clicked');
       // More jQuery code...
    });
});
</script>
Zack Macomber
  • 6,682
  • 14
  • 57
  • 104
  • This code is not programmatically clicking a button, it adds an event handler instead. Also we cannot assume the page is under his control. – derloopkat Apr 12 '14 at 00:06