dcsimg
Results 1 to 12 of 12

Thread: can't set radio button to checked in group via webbrowser control

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    15

    can't set radio button to checked in group via webbrowser control

    Having trouble setting a radio button via a webbrowser control in visual basin .net. This is the HTML code I'm trying to act upon!

    <td align="right">
    Afficher statuts&nbsp;:
    </td>
    <td>

    <label for="Company">
    <input type="radio" id="Services" value="true" name="requestinfo_0" />
    Company
    </label>

    <label for="Partener">
    <input type="radio" id="Workload" value="false" name="requestinfo_0" />
    Partener
    </label>

    <label for="All">
    <input type="radio" id="All" value="" name="requestinfo_0" />
    All
    </label>
    The suggested solution I found searching shows it should work like this! But doesn't!

    WebBrowser1.Document.GetElementById("Workload").SetAttribute("Checked", True)
    But I believe I would need to specify the name as well as from what I can tell its possible to have more than 1 ID as Workload but in a radio button with a different name. But I can't see where or how to write the code accordingly!

    I'm doing this in Visual studio 2019 in Visual basic!

    Thanks Pete
    Last edited by peterg2000; Nov 22nd, 2019 at 07:55 AM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,119

    Re: can't set radio button to checked in group via webbrowser control

    Try

    SetAttribute("Checked", "Checked")

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    15

    Re: can't set radio button to checked in group via webbrowser control

    Quote Originally Posted by .paul. View Post
    Try

    SetAttribute("Checked", "Checked")


    Same result! I've tried different options , SetAttribute("Checked", "True") or SetAttribute("Checked", True).

    I`m wondering if I`d have to click the button instead of setting it! Not sure of the coding though!


    Pete

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,119

    Re: can't set radio button to checked in group via webbrowser control

    I just tried it using just the code you posted, and it worked fine. The id of each element should be unique. There should not be more than one element with the id "Workload" in your page.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,119

    Re: can't set radio button to checked in group via webbrowser control

    You need to be sure your page is fully loaded before setting it...

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Document.GetElementById("Workload").SetAttribute("Checked", True)
    End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    15

    Re: can't set radio button to checked in group via webbrowser control

    Quote Originally Posted by .paul. View Post
    I just tried it using just the code you posted, and it worked fine. The id of each element should be unique. There should not be more than one element with the id "Workload" in your page.
    Yeah , my bad as I was fooled by having one Workload and the other workload. Missed the case change on the W.

    Pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    15

    Re: can't set radio button to checked in group via webbrowser control

    The code to change the Radio button is in the sub pointed to by addhandler statement! So I know the page has loaded!

    Pete

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,119

    Re: can't set radio button to checked in group via webbrowser control

    If that’s the wb_documentcompleted method, you’ll find it fires before the page is ready. Search for readystate

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    15

    Re: can't set radio button to checked in group via webbrowser control

    The code to change the Radio button is in the sub pointed to by addhandler statement! So I know the page has loaded!

    Pete

  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    15

    Re: can't set radio button to checked in group via webbrowser control

    I have a second part to this code that requests a download of an Excel file once the radio boxes are selected! And it fires properly! Its right after the WebBrowser1.Document.GetElementById("Workload").SetAttribute("Checked", True)

    Dim teststr As String
    For Each el As HtmlElement In els teststr = el.GetAttribute("href")
    'Debug.Print(el.GetAttribute("href"))
    If teststr.Contains("/exportExcelDetailled") Then
    el.InvokeMember("click")
    End If
    Next

    Pete
    Last edited by peterg2000; Nov 25th, 2019 at 07:25 AM.

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    15

    Re: can't set radio button to checked in group via webbrowser control

    Solved the problem by using a click event! Works fine now!


    For Each curElement As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
    If curElement.GetAttribute("type") = "radio" AndAlso curElement.GetAttribute("id") = "Services" Then
    curElement.SetAttribute("checked", "checked")

    End If
    If curElement.GetAttribute("type") = "radio" AndAlso curElement.GetAttribute("id") = "Radio3" Then
    curElement.SetAttribute("checked", "checked")
    End If
    If curElement.GetAttribute("type") = "radio" AndAlso curElement.GetAttribute("id") = "all" Then
    curElement.SetAttribute("checked", "checked")
    End If
    Next


    Pete

  12. #12

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    15

    Re: can't set radio button to checked in group via webbrowser control

    Thanks to those who tried to help!

    Pete
    Last edited by peterg2000; Nov 25th, 2019 at 08:38 AM. Reason: Double post!

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width