简体   繁体   English

通过WebBrowser控件取消选中网页中的复选框

[英]Uncheck a checkbox in webpage through WebBrowser control

Is there a way to uncheck/check a checkbox within a webpage that is loaded within a webbrowser control? 有没有一种方法可以取消选中/选中Web浏览器控件中加载的网页中的复选框? Thank you. 谢谢。

Update: (This is what I originally tried with no luck) 更新:(这是我本来没有运气的尝试)

HtmlDocument rememberme = this.webBrowser1.Document;
rememberme.GetElementById("remBox").SetAttribute("checked", "false");

You can use: 您可以使用:

webBrowser.Document.InvokeScript

see: 看到:

InvokeScript InvokeScript

This way you can call JS function that will do what you want to the page. 这样,您可以调用JS函数,该函数将对页面执行所需的操作。

Another way is to use mshtml API, like this: ( ( HTMLInputElement )this.webBrowser1.Document.GetElementById( "test" ).DomElement ).@checked = false; 另一种方法是使用mshtml API,如下所示: ( ( HTMLInputElement )this.webBrowser1.Document.GetElementById( "test" ).DomElement ).@checked = false;

You can use the InvokeMember Method of a HTML Element to invoke a DOM Event. 您可以使用HTML元素的InvokeMember方法来调用DOM事件。 In our case "click". 在我们的例子中是“点击”。 If you have a WebBrowser Control than all you have to do is find the element and Invoke the Method. 如果您拥有WebBrowser控件,那么您所要做的就是找到元素并调用方法。

For Each ele As HtmlElement In Me.WebBrowser1.Document.All
        If ele.Name = "accept" Then
            ele.InvokeMember("click")
        End If
Next

In my example 'accept' was the name of the checkbox element. 在我的示例中,“接受”是复选框元素的名称。 You can use the SetAttribute() for some other controls but the best way to emulate a click is to find what the checkbox state is by using GetAttribute() and click if the checkbox is set to false. 您可以将SetAttribute()用于其他一些控件,但是模拟单击的最佳方法是使用GetAttribute()查找复选框的状态,然后单击复选框是否设置为false。 Hope it helps. 希望能帮助到你。

在javascript中,您应该可以使用clientId设置.checked = false。

http://msdn.microsoft.com/zh-cn/library/system.windows.forms.htmldocument.invokescript.aspx,您需要分析页面结构以找出如何定位复选框并使用Myles的方法进行检查/取消选中javascript中的复选框。

The LONG version is that you'll have to use DOM functions to find the client ID of the control you want to update, then just set it's value. LONG版本是,您必须使用DOM函数来查找要更新的控件的客户端ID,然后只需设置其值即可。

I can probably find some sample code for you once I get to work if no one else beats me to it... 如果没有其他人能打败我,我可能会在上班后为您找到一些示例代码...

EDIT: 编辑:

Basically all you need to do is: 基本上,您需要做的是:

HTMLDocumentClass doc = webBrowserControl.Document as mshtml.HTMLDocumentClass;

HTMLInputElement input = doc.all.item("checkboxID", 0) as HTMLInputElement;

input.value ="false";  //Could be 0, not sure... 
//could also be input.@checked = false, sorry I haven't actually used this on a checkbox before... one of these should work though

So yeah, not sure why anyone would go through all the effort to write a JavaScript and invoke it when you can set a control's value with 3 lines of code. 所以,是的,不确定当您可以用3行代码设置控件的值时,为什么有人会全力以赴编写JavaScript并调用它。

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

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