I did something similar once. It was terrible, but it works.
You need to add a link to Microsoft.mshtml .
Then you can use IHTMLDocument2 . Why 2? Good question ... anyway, I wrote a couple of helper functions like this:
public static void FillField(object doc, string id, string value) { var element = findElementByID(doc, id); element.setAttribute("value", value); } public static void ClickButton(object doc, string id) { var element = findElementByID(doc, id); element.click(); } private static IHTMLElement findElementByID(object doc, string id) { IHTMLDocument2 thisDoc; if (!(doc is IHTMLDocument2)) return null; else thisDoc = (IHTMLDocument2)doc; var element = thisDoc.all.OfType<IHTMLElement>() .Where(n => n != null && n.id != null) .Where(e => e.id == id).First(); return element; }
JS Execution
private static void ExecuteScript(object doc, string js) { IHTMLDocument2 thisDoc; if (!(doc is IHTMLDocument2)) return; else thisDoc = (IHTMLDocument2)doc; thisDoc.parentWindow.execScript(js); }
I call them like that ...
HtmlDocumentHelper.FillField(webBrowser.Document, <id>, <value>); HtmlDocumentHelper.FillField(webBrowser.Document, <id>, <value>); HtmlDocumentHelper.ClickButton(webBrowser.Document, <id>); HtmlDocumentHelper.ExecuteScript(webBrowser.Document, "alert(1);");
source share