locked
Option button click on IE is not working RRS feed

  • Question

  • I am trying to play around with Excel VBA code to select an option button on a webpage. For some reason the option button is not getting selected even though there is no errors when executing the code. Please share your thoughts.

    Excel VBA code block given below.

    Sub Voting()
    
    Dim MyHTML_Element As IHTMLElement
    Dim MyURL As String
    Dim x As Object
    
    MyURL = "https://www.surveymonkey.com/r/?sm=%2bZAmxFYpsYG1R61ELcYA6g%3d%3d"
    Set MyBrowser = New InternetExplorer
    
    With MyBrowser
       .Silent = True
       .navigate MyURL
       .Visible = True
    
    Do
    Loop Until .readyState = READYSTATE_COMPLETE
    
    Do Until Not x Is Nothing
        Set x = .document.getElementById("ID of one of the option button")
    Loop
    Set x = Nothing
    
        .document.getElementById("ID of one of the option button").Click
    
    Set HTMLDoc = .document
    For Each MyHTML_Element In HTMLDoc.getElementsByTagName("button")
        If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
    Next
    Set HTMLDoc = Nothing
    
    End With
    MyBrowser.Quit
    Set MyBrowser = Nothing
        
    End Sub
    Wednesday, January 21, 2015 4:01 PM

Answers

  • It appears the classList method is missing but the className method exists so you can do this.

    Set x = IE.document.getElementById("748684207_8506939399").NextSibling.NextSibling
    x.classname = x.classname & " checked"


    • Edited by mogulman52 Monday, January 26, 2015 7:30 PM
    • Marked as answer by Balaji_ Monday, January 26, 2015 9:18 PM
    Monday, January 26, 2015 7:13 PM

All replies

  • Hi Balaji,

    According to your description, this issue is more related to VBA. Since there are more VBA experts in the VBA forum, we will move this thread to VBA forum.

    Thanks for your understanding.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 22, 2015 9:50 AM
  • Ok. Thank you.
    Thursday, January 22, 2015 8:39 PM
  • You must not be hitting the button.  Please see this example.

    http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html

    Also, to dump everything from the site to Excel, try this.

    Sub DumpData()
    
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    
    URL = "http://finance.yahoo.com/q?s=sbux&ql=1"
    
    'Wait for site to fully load
    IE.Navigate2 URL
    Do While IE.Busy = True
       DoEvents
    Loop
    
    RowCount = 1
       
    With Sheets("Sheet1")
       .Cells.ClearContents
       RowCount = 1
       For Each itm In IE.document.all
          .Range("A" & RowCount) = itm.tagname
          .Range("B" & RowCount) = itm.ID
          .Range("C" & RowCount) = itm.classname
          .Range("D" & RowCount) = Left(itm.innertext, 1024)
    
          RowCount = RowCount + 1
       Next itm
    End With
    End Sub

    Joel, who frequents these forums, gave me that a long time ago.  It's server me well for many projects.


    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

    Saturday, January 24, 2015 5:04 PM
  • Thanks for the response.

    Just to make it more clear, when I step through my code which I have posted I cannot see the option button getting selected. There is no error thrown, but I cannot see the option button getting selected in the browser.

    .document.getElementById("748684207_8506939402").Click

    Sunday, January 25, 2015 10:05 AM
  • You have to set the value of an option button.

    document.getElementsById("748684207_8506939399").value="8506939399";
    Sunday, January 25, 2015 2:19 PM
  • I just removed a couple lines of code and it worked fine for me.

    Sub Voting()
    
    Dim MyHTML_Element As IHTMLElement
    Dim MyURL As String
    Dim x As Object
    
    MyURL = "https://www.surveymonkey.com/r/?sm=%2bZAmxFYpsYG1R61ELcYA6g%3d%3d"
    Set MyBrowser = New InternetExplorer
    
    With MyBrowser
       .Silent = True
       .navigate MyURL
       .Visible = True
    
    Do
    Loop Until .readyState = READYSTATE_COMPLETE
    
    Set HTMLDoc = .document
    For Each MyHTML_Element In HTMLDoc.getElementsByTagName("button")
        If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
    Next
    Set HTMLDoc = Nothing
    
    End With
    MyBrowser.Quit
    Set MyBrowser = Nothing
        
    End Sub
    


    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

    Sunday, January 25, 2015 4:15 PM
  • Thanks for the response. I tried this also, but when I step through the code I cannot see the option button getting selected. Please step through the code and let me know if you are able to see the option button getting selected when you execute this line.

    document.getElementsById("748684207_8506939399").value="8506939399";

    Sunday, January 25, 2015 5:20 PM
  • Thanks for the response. You have removed the code which selects the option button. That is the part of the code which is having issue and I need some assistance. I tried both the below methods, but it is not working. When I step through the code I cannot see the option button getting selected in the browser.

    document.getElementsById("748684207_8506939399").Click

    document.getElementsById("748684207_8506939399").value="8506939399"

    Sunday, January 25, 2015 5:24 PM
  • If you use a tool like Firebug you see it is adding a class 'checked' to a label (in bold).  There is no id on the label so you will have to navigate the document to get to the label.  I use jquery so I don't know how to navigate using just Javascript.

    <div class="radio-button-container" data-sm-radio-button="">
    <input id="748684207_8506939399" class="radio-button-input checked" type="radio" value="8506939399" name="748684207">
    <label class="answer-label radio-button-label clearfix checked" for="748684207_8506939399" data-sm-radio-button-label="">
    <span class="radio-button-display"></span>
    <span class="radio-button-label-text question-body-font-theme user-generated"> SSJ06 - Pravasthi </span>
    </label>
    </div>



    • Edited by mogulman52 Monday, January 26, 2015 3:28 PM
    Sunday, January 25, 2015 6:27 PM
  • I took a break and played with Firebug for 10 minutes or so.  Here is how you do it:

    document.getElementById("748684207_8506939399").nextSibling.nextSibling.classList.add("checked");

    The nextSibling.nextSibling gets you to the label.  I added checked to the class list.

    Sunday, January 25, 2015 9:43 PM
  • Thanks mogulman52 for the response. Should I use javascript with my Excel VBA code? This can't be done with Excel VBA code itself?
    Monday, January 26, 2015 3:59 PM
  • It appears the classList method is missing but the className method exists so you can do this.

    Set x = IE.document.getElementById("748684207_8506939399").NextSibling.NextSibling
    x.classname = x.classname & " checked"


    • Edited by mogulman52 Monday, January 26, 2015 7:30 PM
    • Marked as answer by Balaji_ Monday, January 26, 2015 9:18 PM
    Monday, January 26, 2015 7:13 PM
  • Excellent. It works a treat. Many Thanks mogulman52.
    Monday, January 26, 2015 9:18 PM