简体   繁体   English

如何使用C#以编程方式单击WebBrowser中的文本输入

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

I have opened a website using WebBrowser. 我已经使用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: 我无法使用焦点,因为此网站仅在单击该字段时使用JS来解锁该字段,并且我也尝试过以下操作:

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

But it returns mi = null. 但它返回mi = null。 How to do this so it will work? 如何做到这一点将起作用?

Very similar to my answer on your other question . 与我关于另一个问题的回答非常相似。

Get an HtmlElement respresentative of your textbox, and call HtmlElement.InvokeMember("click") on it. 获取代表您的文本框的HtmlElement,然后在其上调用HtmlElement.InvokeMember(“ click”)。

If you can, use: 如果可以,请使用:

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

or something similar. 或类似的东西。 For me, it's been much easier and more accurate. 对我来说,它变得更加容易和准确。

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'); 您还可以使用document.getElementById('buttonID'); instead of document.getElementsByTagName('button')[1]; 而不是document.getElementsByTagName('button')[1]; but an id must be provided for this button on that particular webpage. 但必须在该特定网页上为此按钮提供一个ID。

在HtmlElement或Browser.InvokeScript函数上使用InvokeMethhod。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM