Lost_In_Library
Lost_In_Library

Reputation: 3483

How to Click A Button Programmatically - Button in WebBrowser ( IE )

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>

Upvotes: 4

Views: 9448

Answers (2)

Zack Macomber
Zack Macomber

Reputation: 6905

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>

Upvotes: -1

David
David

Reputation: 218798

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.

Upvotes: 8

Related Questions