Tom Smykowski
Tom Smykowski

Reputation: 26089

How to programmatically click on text input in WebBrowser using C#

I have opened a website using WebBrowser. Now I would like to programmatically click input text (textbox) field. I can not use focus because this website uses JS to unlock this field only if it's clicked and I've tried also this:

Object obj = ele.DomElement;
System.Reflection.MethodInfo mi = obj.GetType().GetMethod("click");
mi.Invoke(obj, new object[0]); 

But it returns mi = null. How to do this so it will work?

Upvotes: 0

Views: 5538

Answers (4)

Ehsan
Ehsan

Reputation: 11

To fill-up a text field on a webpage:

string code ="";
code = code + "var MyVar=document.getElementById('tbxFieldNameOnWebPage');if(MyVar != null) MyVar.value = 'SOMEVALUE';";
domDocument.parentWindow.execScript(code, "JScript");

Then To Click a button on a webpage:

code = "";
code = "var SignupFree = document.getElementsByTagName('button')[1];";
code = (code + " SignupFree.click();");
domDocument.parentWindow.execScript(code, "JScript");

you can also use document.getElementById('buttonID'); instead of document.getElementsByTagName('button')[1]; but an id must be provided for this button on that particular webpage.

Upvotes: 1

Eyal
Eyal

Reputation: 5848

If you can, use:

webbrowser1.Navigate("javascript:document.forms[0].submit()")

or something similar. For me, it's been much easier and more accurate.

Upvotes: 1

Cam Soper
Cam Soper

Reputation: 1509

Very similar to my answer on your other question.

Get an HtmlElement respresentative of your textbox, and call HtmlElement.InvokeMember("click") on it.

Upvotes: 2

Alex Reitbort
Alex Reitbort

Reputation: 13696

Use InvokeMethhod on HtmlElement or Browser.InvokeScript function.

Upvotes: 0

Related Questions