c# - WebBrowser OpenDialog automation fails in .NET -
i press button after webpage loads webbrowser control. how know it's loaded? wait significant amount of time sure.
here 1 version of button code :
var elementsx = webbrowser1.document.getelementsbytagname("input"); foreach (htmlelement file in elementsx) { if (file.getattribute("type") == "file") { listbox1.items.add(file.style.tostring()); file.focus(); file.invokemember("click"); sendkeys.send(@"c:\users\john\desktop\test1\blue-book-motorcycle.jpg" + "{enter}"); } }
here another:
mshtml.htmldocument doc = (mshtml.htmldocument)webbrowser1.document.domdocument; // ################################################################################ //get title textbox ihtmlelementcollection inputer = (ihtmlelementcollection)doc.getelementsbytagname(@"input"); foreach (ihtmlelement element in inputer) { listbox1.items.add(element.style.csstext + " ||| " + element.getattribute("type").tostring() + "%%%" + element.classname); //we other textboxes similar class names begin //gwt-textbox test it. if(element.style.csstext=="height: 0px; visibility: hidden; position: absolute;" && element.getattribute("type").tostring()=="file") { ++i; if(i==3) { element.click(); sendkeys.send(@"c:\users\john\desktop\test1\blue-book-motorcycle.jpg" + "{enter}"); } } //webbrowser1.update(); }
they both fail in sense that, though open dialog appears , no keys entered open dialog text box , sits there doing nothing.
also, if click cancel button opendialog following error: ":\users\john\desktop\test1\blue-book-motorcycle.jpg filename not valid" . so, opendialog textbox stripped "c" keys. what's problem? ran admin also. fail!!!
you failing on because trying put text modal dialog thread of webbrowser's form. won't succeed doing this. if want fill file name on dialog , press enter key, need create thread , send keys need.
try this:
private void webbrowser_documentcompleted(object sender, webbrowserdocumentcompletedeventargs e) { var elementsx = webbrowser1.document.getelementsbytagname("input"); foreach (htmlelement file in elementsx) { if (file.getattribute("type") == "file") { listbox1.items.add(file.style.tostring()); file.focus(); file.invokemember("click"); task.delay(500).continuewith(t => sendfilename(@"c:\users\john\desktop\test1\blue-book-motorcycle.jpg"), taskscheduler.fromcurrentsynchronizationcontext()); } } } private void sendfilename(string filename) { sendkeys.send(filename + "{enter}"); }
Comments
Post a Comment