Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Interacting With Radio Buttons On Webpage

EDIT: A few months later I attempted this again using the same syntax I had tried the first time (posted below). For some reason, it worked! Maybe something other than my syntax was causing the ineffectiveness... END EDIT

I've been searching forums for a couple of hours now trying to find a solution to this problem, but none of the things I've tried work.

I'm using VBA to automate the process of creating a survey on SurveyMonkey. So far I've been able to:

  • Log into my account,
  • Click several hyperlinks to create a new response collector,
  • Name the collector,
  • Go to the collector settings,
  • And, select three of four radio buttons to change collector settings.

The issue isn't that I can't select the radio buttons; my code selects the first three buttons just fine. What puzzles me is that the fourth button doesn't change! I use the same process for each button, so I can't figure out why the last button won't select.

Here's the section of my code:

objIE.Document.getElementById("rdlResponseType_1").Click    'Allow multiple responses = Yes
objIE.Document.getElementById("rdlResponseEdit_1").Click    'Allow Responses to be Edited = Yes
objIE.Document.getElementById("rdlThankyou_1").Click        'Display a "Thank You" page? = Yes
objIE.Document.getElementById("rdlCompleteOpt_1").Click     'Survey Completion = Close Window

This is the HTML for the radio buttons:

<table border="0" cellpadding="0" cellspacing="0" style="width: 100%;">
                        <tr id="CompleteOptDesc">
            <td style="">&nbsp;</td>
            <td>After the respondent leaves the survey:</td>
        </tr>

                        <tr id="CompleteOptItems">
            <td style="">&nbsp;</td>
            <td align="left" nowrap="nowrap" valign="top">
                                <table id="rdlCompleteOpt" class="Clean radioList" OnClick="radioToggle('rdlCompleteOpt', '0', 'panLink');" name="rdlCompleteOpt" border="0" style="white-space: nowrap">
                <tr>
                    <td><span style="white-space: nowrap"><input id="rdlCompleteOpt_0" type="radio" name="rdlCompleteOpt" value="0" checked="checked" /><label for="rdlCompleteOpt_0"><b>Redirect</b> to your own webpage.</label></span></td>
                </tr><tr>
                    <td><input id="rdlCompleteOpt_1" type="radio" name="rdlCompleteOpt" value="2" /><label for="rdlCompleteOpt_1"><b>Close Window</b></label></td>
                </tr>
            </table>

Here is the code for the toggle section of the radio:

<td width="65%" valign="top">
                                <div id="panLink" style="DISPLAY:inline">

                                    <div class="URLinfo">

                                            <b>Enter a URL</b> to jump to upon leaving the survey:<br />
                                            <div title="REQUIRED: Enter URL (255 character max)" style="padding:4px 0 2px 0;">
                                                <input name="txtWebLink" type="text" value="http://www.surveymonkey.com/" maxlength="255" size="40" id="txtWebLink" class="RQR opaque" />
                                            </div>
                                            <span class="tip">Example: <u>http://www.mysite.com/home.html</u></span>
                                    </div>

            </div>
                            </td>

Any suggestions would be very appreciated!

like image 241
ARich Avatar asked Nov 04 '22 01:11

ARich


1 Answers

There's a couple things to consider:

  1. When are you calling this code? OnNavigate or DocumentComplete? It may be that the object doesn't exist yet. You can check to see if it does by using a breakpoint and adding a watch for the expressions

    objIE.Document.getElementById("rdlCompleteOpt_1")
    

    Use OnDocumentComplete to ensure the HTML components exist

  2. It's possible the way you are calling the click function is causing an issue. Try this

    Call objIE.Document.getElementById("rdlCompleteOpt_1").Click()
    

    I doubt that it is likely to have much impact but I like to verify everything.

  3. Isolate the cause of the issue by removing the 'layers'. Can you accomplish this using pure JavaScript that's invoked from the IE dev tools (F12) JavaScript console?

    If no then the issue is with how you're interacting with the page elements. If yes then we need to figure out what it is with the VBA intaction is causing an issue. Again see #2 as it is likely the cause of the issue.

    Also, does the issue occur if you change the HTML order of the elements? What if you call it this way?

    objIE.Document.getElementById("rdlCompleteOpt_1").Click     'Survey Completion = Close Window
    objIE.Document.getElementById("rdlResponseType_1").Click    'Allow multiple responses = Yes
    objIE.Document.getElementById("rdlResponseEdit_1").Click    'Allow Responses to be Edited = Yes
    objIE.Document.getElementById("rdlThankyou_1").Click        'Display a "Thank You" page? = Yes
    

    Does it work then or does the "last" item fail?

  4. Highly unlikely to be the cause but the HTML supplies is missing the code to close cell, row and table.

            </tr>
    
        </table>
       <!-- missing -->
       </td>
      </tr>
    </table>
    
  5. While not the specific answer to your question I believe this may be inline with the intent of your post. Since you want to manage a surveymonkey collector take a look at the SurveyMonkey API here as using an API gives you quite a few benefits.

    • you don't have to rely on user elements which may change or be changed outside of your control
    • you can use a controlled and "contracted" interface to manage the information which covers you for any future changes (assuming they don't deprecate and stop supporting the API)
like image 116
Shawn E Avatar answered Nov 10 '22 14:11

Shawn E