How to click a button on an ASP.NET web page programmatically?

前端 未结 9 1362
既然无缘
既然无缘 2021-02-09 10:06

I am trying to figure out how to click a button on a web page programmatically.

Specifically, I have a WinForm with a WebBrowser control. Once it navigates to the targe

相关标签:
9条回答
  • 2021-02-09 10:49

    try this button.focus System.Windows.Forms.SendKeys.Send("{ENTER}")

    0 讨论(0)
  • 2021-02-09 10:49

    You could call the method directly and pass in generic object and EventArgs parameters. Of course, this might not work if you were looking at the sender and EventArgs parameters for specific data. How I usually handle this is to refactor the guts of the method to a doSomeAction() method and the event handler for the button click will simply call this function. That way I don't have to figure out how to invoke what is usually just an event handler to do some bit of logic on the page/form.

    In the case of javascript clicking a button for a form post, you can invoke form.submit() in the client side script -- which will run any validation scripts you defined in the tag -- and then parse the Form_Load event and grab the text value of the submit button on that form (assuming there is only one) -- at least that's the ASP.NET 1.1 way with which I'm very familiar... anyone know of something more elegant with 2.0+?

    0 讨论(0)
  • 2021-02-09 10:56

    You can try and invoke the Page_ClientValidate() method directly through the clientscript instead of clicking the button, let me dig up an example.

    Using MSHTML

    mshtml.IHTMLWindow2 myBroserWindow = (mshtml.IHTMLWindow2)MyWebBrowser.Document.Window.DomWindow;
    myBroserWindow.execScript("Page_ClientValidate();", "javascript");
    
    0 讨论(0)
提交回复
热议问题