locked
Unclick a checkbox programmaticly

    Question

  • Hi,
    I am trying to unclick a check box (on a website) programmaticly, the checkbox is set , by default, to "checked" and I need to uncheck it.
    Here is the html:

    Code Snippet

    <input type="checkbox" name="PersistentCookie" id="PersistentCookie"value="yes"/>
    &nbsp;<input type="hidden" name='rmShown' value="1"/>


     And here is the code I have:

    Code Snippet

    Dim PersistCookie As HtmlElementCollection = _ WebBrowser1.Document.GetElementsByTagName("input")
    For Each curElement As HtmlElement In PersistCookie
    Dim controlName As String = curElement.GetAttribute("name").ToString
    If controlName = "PersistentCookie" Then
    curElement.SetAttribute("Checked", False)
    End If
    Next
    Dim Rm As HtmlElementCollection = _ WebBrowser1.Document.GetElementsByTagName("input")
    For Each curElement As HtmlElement In Rm
    Dim controlName As String = curElement.GetAttribute("name").ToString
    If controlName = "rmShown" Then
    curElement.SetAttribute("value", "0")
    End If
    Next


    I cant get it to work.


    Wednesday, October 15, 2008 6:34 PM

Answers

  • Try this.

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            WebBrowser1.Navigate("c:\3.htm")
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim btnLogin As HtmlElementCollection = Me.WebBrowser1.Document.GetElementsByTagName("input")
            For Each curElement As HtmlElement In btnLogin
                Select Case curElement.Name
                    Case "PersistentCookie1"
                        curElement.InvokeMember("click")
                    Case "PersistentCookie2"
                        curElement.InvokeMember("click")
                    Case "PersistentCookie3"
                        curElement.InvokeMember("click")
                End Select
            Next
        End Sub
    End Class

    '3.htm
    <html>
    <head>
    <title>Testing</title>
    </head>
    <body>
    <table rules="none" border="1" frame="box">
    <tr>
    <td><Label id="verify">Test Text:</Label></td>
    <td width="233"><INPUT id="passedText" type="text" maxLength="25" size="82" ></td>
    </tr>
    </table>
    <input type="checkbox" name="PersistentCookie1" id="PersistentCookie"value="yes"/>
    <br/>
    <input type="checkbox" name="PersistentCookie2" id="PersistentCookie"value="yes"/>
    <input type="checkbox" name="PersistentCookie3" id="PersistentCookie"value="yes"/>
    </body>
    </html>

    Monday, October 20, 2008 4:27 AM

All replies

  • I would recommend that you use htmlelement.focus to set the focus to checkbox. Then use the Sendkeys to send Enter or space key to modify the selection. Try it.

    Friday, October 17, 2008 5:44 AM
  • Thanks for the reply Riquel,
    It works fine (for just one checkbox) however I have 3 checkboxes on the page that need to be unchecked and using the code for each checkbox only unchecks the first...what am I doing wrong?

    I tried:

    Code Snippet

    Dim CheckBox1 As HtmlElementCollection = _ WebBrowser1.Document.GetElementsByTagName("input")
    For Each curElement As HtmlElement In
    CheckBox1
    Dim controlName As String = curElement.GetAttribute("name").ToString
    If controlName = "PersistentCookie" Then
    curElement.Focus()
    SendKeys.Send(" ")
    End If
    Next


    Dim CheckBox2 As HtmlElementCollection = _ WebBrowser1.Document.GetElementsByTagName("input")
    For Each curElement As HtmlElement In CheckBox2
    Dim controlName As String = curElement.GetAttribute("name").ToString
    If controlName = "ctnh" Then
    curElement.Focus()
    SendKeys.Send(" ")
    End If
    Next


    Dim CheckBox3 As HtmlElementCollection = _ WebBrowser1.Document.GetElementsByTagName("input")

    For Each curElement As HtmlElement In CheckBox3
    Dim controlName As String = curElement.GetAttribute("name").ToString
    If controlName = "sdfr" Then
    curElement.Focus()
    SendKeys.Send(" ")
    End If
    Next




    Thanks

    Friday, October 17, 2008 7:34 AM
  • Try this.

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            WebBrowser1.Navigate("c:\3.htm")
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim btnLogin As HtmlElementCollection = Me.WebBrowser1.Document.GetElementsByTagName("input")
            For Each curElement As HtmlElement In btnLogin
                Select Case curElement.Name
                    Case "PersistentCookie1"
                        curElement.InvokeMember("click")
                    Case "PersistentCookie2"
                        curElement.InvokeMember("click")
                    Case "PersistentCookie3"
                        curElement.InvokeMember("click")
                End Select
            Next
        End Sub
    End Class

    '3.htm
    <html>
    <head>
    <title>Testing</title>
    </head>
    <body>
    <table rules="none" border="1" frame="box">
    <tr>
    <td><Label id="verify">Test Text:</Label></td>
    <td width="233"><INPUT id="passedText" type="text" maxLength="25" size="82" ></td>
    </tr>
    </table>
    <input type="checkbox" name="PersistentCookie1" id="PersistentCookie"value="yes"/>
    <br/>
    <input type="checkbox" name="PersistentCookie2" id="PersistentCookie"value="yes"/>
    <input type="checkbox" name="PersistentCookie3" id="PersistentCookie"value="yes"/>
    </body>
    </html>

    Monday, October 20, 2008 4:27 AM
  • Thanks Riquel,
    That works perfectly!

    I have learnt so much on this forum...still so much to learn though!
    Monday, October 20, 2008 11:47 AM