简体   繁体   中英

How do I click a button on an embedded web control from C#?

There is a page loaded in a webBrowser1 object. It has a "button" tag, which is the button that I need to click. It's basic button. No JavaScript, etc.

I tried:

1) to click it with "enter":

webBrowser.Document.GetElementsByTagName("button")[0].Focus();
Sendkeys.Sendwait("{ENTER}");

That didn't work.

2) to call the "click" event:

webBrowser.Document.GetElementsByTagName("button")[0].InvokeMember("click");

That also didn't work because it's not JavaScript.

3) the "click" handler:

webBrowser.Document.GetElementsByTagName("button")[0].Click +=
        new System.EventHandler(button_click1);

This also can not work, because from what I've read, this is an event handler which triggers after the user actually clicks on this button.

So I ran out of options. What else can I try?

you can try like this for capturing the button event in web browser using c#

HtmlElement el = webBrowser1.Document.All["mybutton"];
object obj = el.DomElement;
System.Reflection.MethodInfo mi = obj.GetType().GetMethod("click");
mi.Invoke(obj, new object[0]); 

OR

private void Form1_Load(object sender, EventArgs e)
{
   this.wb.Url = new Uri("https://login.yahoo.com/config/login_verify2?.intl=gr&.src=ym");
   this.wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
public void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    HtmlElement username = this.wb.Document.All["username"];
    username.SetAttribute("value", "kataras2012@yahoo.gr");
    HtmlElement pass = this.wb.Document.All["passwd"];
    pass.SetAttribute("value", "passTest");
    HtmlElement goButton = this.wb.Document.All[".save"];
    goButton.InvokeMember("click");

    return;
}

i hope it will helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM