Uncheck a checkbox in webpage through WebBrowser control
Is there a way to uncheck/check a checkbox within a web开发者_如何学Cpage that is loaded within a webbrowser control? 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
This way you can call JS function that will do what you want to the page.
Another way is to use mshtml API, like this:
( ( HTMLInputElement )this.webBrowser1.Document.GetElementById( "test" ).DomElement ).@checked = false;
You can use the InvokeMember Method of a HTML Element to invoke a DOM Event. In our case "click". If you have a WebBrowser Control than all you have to do is find the element and Invoke the Method.
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. Hope it helps.
In javascript you should be able to use the clientId to set .checked = false.
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.invokescript.aspx you need to analyze the page structure to find out how to locate the checkbox and use Myles's method to check/uncheck the checkbox in 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.
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.
精彩评论