简体   繁体   中英

how do I submit a form in a WebBrowser control?

how to programmatically set an input an element value inside a WebBrowser control?

For example, I have an HTML page, like:

<form method="post" action="...aspx" enctype="multipart/form-data" id="mainForm">
    <input type="file" id="file" />
    <input type="submit" id="submit" value="Submit it" />
</form>

How do I to submit it via C# code? I tried something:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
{
   var doc = webBrowser1.Document;
   var input = doc.GetElementById("file");
   input.SetAttribute("value", @"C:\foo.baa");
   doc.GetElementById("mainForm").InvokeMember("submit");
}

but it does not working, the value of input is not setted and the form is not submited. I hope this is clean. Thanks in advance.

Try this:

HtmlElement loBtn = (HtmlElement)loWebBrowser.Document.GetElementById("btnSubmit");
loBtn .InvokeMember("click");

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