Results 1 to 40 of 531

Thread: Manipulate/Change/Form Fill data in webpages using the Webbrowser control

Hybrid View

  1. #1
    Member
    Join Date
    Feb 2008
    Posts
    39

    Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control

    here is my html sample
    I'm trying to simulator automate filling of data to the webBrowser:
    <html>
    <head>
    <form name="input" action="html_form_action.asp" method="get">
    Username: <input type="text" name="user">
    <br><input type="radio" name="sex" value="male"> Male
    <br><input type="radio" name="sex" value="female"> Female
    <br><input type="checkbox" name="vehicle" value="Bike">I have a bike
    <br><input type="checkbox" name="vehicle" value="Car">I have a car
    <br><input type="checkbox" name="vehicle" value="Airplane">I have an airplane
    <br><input type="submit" value="Submit">
    </form>
    </head>
    </html>

    using vs2005, vb.net
    FwebBrowser.Document.GetElementById("sex").SetAttribute("Gender", "male")
    FwebBrowser.Document.GetElementById("vehicle").SetAttribute("Cartype", "Bike")

    I had resolved text boxes, button click events, drop down list but coming to radio buttons and checkbox, I failed.

    I couldn't understand how do I set the rdo btn o chkbox to "checked"
    can someone guide me

  2. #2
    New Member
    Join Date
    Oct 2007
    Posts
    2

    Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control

    I have a little bit of a problem. I did not see that it was covered previously, if it was I apologize.

    I am attempting to create a password manager that automatically saves username and password information from a web page and fills it back in when you revisit the same web page. My dilemma is accurately finding the "User Name" element and "Password" element for each web page. Since every page can have different names, tag-names, etc.

    I have been playing around with a couple of different methods, sometimes they work, with some pages that have multiple "input" elements and I cannot correctly identify the correct elements. My thought process so far has been to find an element on the page who’s type is "password", then find the element right before that that is a standard "input". Does anyone know if there is a way to get this information, and fill it back in accurately with any web page?

    This is the source code that I am playing around with right now. Any ideas? Thanks.

    HTMLDoc = iE7.Document
    iHTMLCol = HTMLDoc.getElementsByTagName("input")

    For Each iHTMLEle In iHTMLCol
    If Not LCase(iHTMLEle.type) = "hidden" Then
    Debug.Print(iHTMLEle.name & " [" & iHTMLEle.type & "]")
    Debug.Print(iHTMLEle.value)
    Debug.Print("")
    End If


    'For iForm = HTMLDoc.forms.length - 1 To 0 Step -1
    'For iElem = HTMLDoc.forms.item(, iForm).elements.length - 1 To 0 Step -1
    'Debug.Print(HTMLDoc.forms.item(, iForm).elements.item(, iElem).type)
    'If HTMLDoc.forms.item(, iForm).elements.item(, iElem).type = "password" Then _
    'HTMLDoc.forms.item(, iForm).elements.item(, iElem).value = "password"

    'If HTMLDoc.forms.item(, iForm).elements.item(, iElem).type = "input" Then
    'HTMLDoc.forms.item(, iForm).elements.item(, iElem).value = "User Name"
    'End If
    'Next iElem
    'Next iForm
    Next

  3. #3
    Member
    Join Date
    Feb 2008
    Posts
    39

    Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control

    Quote Originally Posted by akagi07
    here is my html sample
    I'm trying to simulator automate filling of data to the webBrowser:
    <html>
    <head>
    <form name="input" action="html_form_action.asp" method="get">
    Username: <input type="text" name="user">
    <br><input type="radio" name="sex" value="male"> Male
    <br><input type="radio" name="sex" value="female"> Female
    <br><input type="checkbox" name="vehicle" value="Bike">I have a bike
    <br><input type="checkbox" name="vehicle" value="Car">I have a car
    <br><input type="checkbox" name="vehicle" value="Airplane">I have an airplane
    <br><input type="submit" value="Submit">
    </form>
    </head>
    </html>

    using vs2005, vb.net
    FwebBrowser.Document.GetElementById("sex").SetAttribute("Gender", "male")
    FwebBrowser.Document.GetElementById("vehicle").SetAttribute("Cartype", "Bike")

    I had resolved text boxes, button click events, drop down list but coming to radio buttons and checkbox, I failed.

    I couldn't understand how do I set the rdo btn o chkbox to "checked"
    can someone guide me
    still having troubles with setting values to rdobtn and checkbox..
    can someone advise.

  4. #4

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control

    Quote Originally Posted by akagi07
    still having troubles with setting values to rdobtn and checkbox..
    can someone advise.
    Because your have elements all named the same thing (vehicle and sex) there is no way to simply grab one like you are doing and set a value. Trying to grab just one will just give you the first matching element. You need to grab them all, and loop them until you are on the element you want, then take action on it.

    Radio buttons and Checkboxes often have the same name or id properties, so this is approach needed often when dealing with these.

    First I use this subroutine to check/uncheck a radio or checkbox.

    Code:
        Private Sub ToggleHTMLCheckOrRadio(ByVal Checked As Boolean, ByVal CheckboxOrRadio As HtmlElement)
            Try
                If Checked Then
                    If Not Convert.ToBoolean(CheckboxOrRadio.GetAttribute("checked")) Then
                        CheckboxOrRadio.InvokeMember("click")
                    End If
                Else
                    If Convert.ToBoolean(CheckboxOrRadio.GetAttribute("checked")) Then
                        CheckboxOrRadio.InvokeMember("click")
                    End If
                End If
            Catch ex As Exception
                MessageBox.Show("Error occured toggling checkbox/radio: " & ex.Message)
            End Try
        End Sub
    Then you need to call this code like so:

    Code:
            For Each RadioButton As HtmlElement In WebBrowser1.Document.All.GetElementsByName("sex")
                If RadioButton.GetAttribute("value") = "male" Then
                    ToggleHTMLCheckOrRadio(True, RadioButton)
                    Exit For
                End If
            Next
    
            For Each RadioButton As HtmlElement In WebBrowser1.Document.All.GetElementsByName("vehicle")
                If RadioButton.GetAttribute("value") = "Bike" Then
                    ToggleHTMLCheckOrRadio(True, RadioButton)
                    Exit For
                End If
            Next
    Notice how I grab all elements that match the given name/id. Then I loop them till I find the one with the correct value. Once I find that, I pass that element to the routine to check the checkbox or select the radio by invoking a click on it as if the mouse has clicked it.

  5. #5
    New Member
    Join Date
    Sep 2009
    Posts
    1

    Re: Manipulate/Change/Form Fill data in webpages using the Webbrowser control

    Quote Originally Posted by kleinma View Post
    Because your have elements all named the same thing (vehicle and sex) there is no way to simply grab one like you are doing and set a value. Trying to grab just one will just give you the first matching element. You need to grab them all, and loop them until you are on the element you want, then take action on it.

    Radio buttons and Checkboxes often have the same name or id properties, so this is approach needed often when dealing with these.

    First I use this subroutine to check/uncheck a radio or checkbox.

    Code:
        Private Sub ToggleHTMLCheckOrRadio(ByVal Checked As Boolean, ByVal CheckboxOrRadio As HtmlElement)
            Try
                If Checked Then
                    If Not Convert.ToBoolean(CheckboxOrRadio.GetAttribute("checked")) Then
                        CheckboxOrRadio.InvokeMember("click")
                    End If
                Else
                    If Convert.ToBoolean(CheckboxOrRadio.GetAttribute("checked")) Then
                        CheckboxOrRadio.InvokeMember("click")
                    End If
                End If
            Catch ex As Exception
                MessageBox.Show("Error occured toggling checkbox/radio: " & ex.Message)
            End Try
        End Sub
    Then you need to call this code like so:

    Code:
            For Each RadioButton As HtmlElement In WebBrowser1.Document.All.GetElementsByName("sex")
                If RadioButton.GetAttribute("value") = "male" Then
                    ToggleHTMLCheckOrRadio(True, RadioButton)
                    Exit For
                End If
            Next
    
            For Each RadioButton As HtmlElement In WebBrowser1.Document.All.GetElementsByName("vehicle")
                If RadioButton.GetAttribute("value") = "Bike" Then
                    ToggleHTMLCheckOrRadio(True, RadioButton)
                    Exit For
                End If
            Next
    Notice how I grab all elements that match the given name/id. Then I loop them till I find the one with the correct value. Once I find that, I pass that element to the routine to check the checkbox or select the radio by invoking a click on it as if the mouse has clicked it.


    I try the trick of the checkbox but it did not work with me if you can upload us an example to understand the idea please

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width