c# - Async webBrowser for filling out Form -
i need let webbrowser navigate several different similiar url's of 1 website, fill out form, click on send button, , read next site find out if previous step successfull or not. on success loop have stop.
fist stuck @ getting webbrowser.navigate("url");
work in loop. since page need time load have asynchronous.
i new whole asyncronous stuff, need help.
i have found solution on https://stackoverflow.com/a/15936400/4972402 took , put code filling out form , clicking submit button.
public partial class form1 : form { public struct webbrowserawaiter { private readonly webbrowser _webbrowser; private readonly string _url; private readonly taskawaiter<string> _innerawaiter; public bool iscompleted { { return _innerawaiter.iscompleted; } } public webbrowserawaiter(webbrowser webbrowser, string url) { _url = url; _webbrowser = webbrowser; _innerawaiter = processurlawaitable(_webbrowser, url); } public string getresult() { return _innerawaiter.getresult(); } public void oncompleted(action continuation) { _innerawaiter.oncompleted(continuation); } private taskawaiter<string> processurlawaitable(webbrowser webbrowser, string url) { taskcompletionsource<string> taskcompletionsource = new taskcompletionsource<string>(); var handler = new webbrowserdocumentcompletedeventhandler((s, e) => { // todo: put custom processing of document here taskcompletionsource.setresult(e.url + ": " + webbrowser.document.title); }); webbrowser.documentcompleted += handler; taskcompletionsource.task.continuewith(s => { webbrowser.documentcompleted -= handler; }); webbrowser.navigate(url); return taskcompletionsource.task.getawaiter(); } } public form1() { initializecomponent(); } private void button1_click(object sender, eventargs e) { processurlsasync(new[] { "http://google.com", "http://microsoft.com", "http://yahoo.com" }) .start(); } private task processurlsasync(string[] urls) { return new task(() => { foreach (string url in urls) { var awaiter = new webbrowserawaiter(webbrowser1, url); string result = awaiter.getresult(); messagebox.show(result); } }); } } }
the problem when this:
private task processurlsasync(string[] urls) { return new task(() => { foreach (string url in urls) { var awaiter = new webbrowserawaiter(webbrowser1, url, this.currentkunde); string result = awaiter.getresult(); if (result != "failed") { break; } else { messagebox.show(result); } } }); } private taskawaiter<string> processurlawaitable(webbrowser webbrowser, string url) { taskcompletionsource<string> taskcompletionsource = new taskcompletionsource<string>(); var handler = new webbrowserdocumentcompletedeventhandler((s, e) => { // todo: put custom processing of document here if (webbrowser.document.title == "title") { webbrowser.scripterrorssuppressed = false; webbrowser.document.getelementbyid("name").setattribute("value", this._currentcustomer.displayedname); webbrowser.document.getelementbyid("email").setattribute("value", this._currentcustomer.email); //turns off checkinput() webbrowser.document.getelementsbytagname("form")[2].setattribute("onsubmit", ""); //clicks on submit button webbrowser.document.getelementbyid("sendbutton").invokemember("click"); } else { //do other stuff , return "success" } taskcompletionsource.setresult("failed"); });
how can make handler fired again after ckick, return "success" , stop loop?
Comments
Post a Comment