Using VBA to operate radio buttons on a web page

2019-07-27 22:25发布

问题:

I am a VBA rookie attempting to operate a radio button on the web, but am not having much luck. I've been googling for hours, and have found tons of code snippets that i have been attempting to modify to do the job, with no success whatsoever. Must of it is some variation of:

ie.Document.getElementsByName("name_of_radiobox").Item(0).Checked = True

The radio buttons are two options (Export with A, or Export with B). 'Export with A' is automatically selected, and I need obviously the other one selected. According to the HTML the name of the buttons are both the same, and it appears to be on a form that pops up on the existing page.

How can I get the second button selected? I'm sure I am leaving out some vital information, so please let me know if I need to provide anything else, and I would be happy to. Thank you for any and all help!

EDIT: there is this bit of code, which I believe is a subwindow that pops up (its not a separate windown, but a pane in the existing window):

Sys.WebForms.PageRequestManager._initialize('ctl00$sm1', document.getElementById('aspnetForm'));

Then the code for the box appears like this:

<div class="so_heading">
Export response data to Excel</div>
<div id="ctl00_cp1_pageMessage" class="attention">Click the button to send an email containing an Excel file to the email address </div>
<div id="ctl00_cp1_pagec" class="so_fields">
<span id="ctl00_cp1_exportOption"><input id="ctl00_cp1_exportOption_0" type="radio" name="ctl00$cp1$exportOption" value="text" checked="checked" /><label for="ctl00_cp1_exportOption_0">Export with answer texts</label><br /><input id="ctl00_cp1_exportOption_1" type="radio" name="ctl00$cp1$exportOption" value="label" /><label for="ctl00_cp1_exportOption_1">Export with answer codes</label></span>
<div class="gen_menu">
<input type="submit" name="ctl00$cp1$btExport" value="Export Data" onclick="window.setTimeout(cb.curry(__DisableButton, this), 0);" id="ctl00_cp1_btExport" class="confirmitButton" />
</div>

My code so far (which opens the page, clicks a link on that page, which creates the pane with the radio button:

Dim IE As Object Dim ieDoc As Object Dim Anchor As Object Dim ieAnchors As Object

  Set IE = CreateObject("InternetExplorer.Application")
    IE.navigate "the url to my page"
    IE.Visible = True

  Do While IE.busy: DoEvents: Loop
  Do While IE.ReadyState <> 4: DoEvents: Loop

  Set ieDoc = IE.Document
  Set ieAnchors = ieDoc.Anchors

    For Each Anchor In ieAnchors
        If Anchor.innerHTML = "Export Data..." Then
            Anchor.Click
            Exit For
        End If
    Next Anchor

  Do While IE.busy: DoEvents: Loop
  Do While IE.ReadyState <> 4: DoEvents: Loop

    ' the bit I can't get to work to toggle the radio button
    ieDoc.getElementsByName("ctl00$cp1$exportOption").Item(1).Checked = True

End Function

回答1:

I was able to get it to work with the following code:

  Set ieRadio = IE.Document.all
    ieRadio.Item("ctl00$cp1$exportOption")(1).Checked = True

Thank you all for your help!



回答2:

The easiest way is to use that code in server side because VBScript has some problems with the type of browser , html5 ... etc Here is an example of using radiobuttons on server side:

default.aspx

 <!DOCTYPE html>
    <%@ Page Language="vb" AutoEventWireup="false" %>
    <script runat="server" language="vb">
      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Radio1.Checked = False
            Radio2.Checked = True
      End Sub
    </script>
    <html>
      <body>
        <form id="Form1" runat="server">
          <input id="Radio1" type="radio" value="ExA" name="radbtnEx" runat="server" />Export with A<br />
          <input id="Radio2" type="radio" value="ExB" name="radbtnEx" runat="server" />Export with B<br />
        </form>
      </body>
    </html>

if that won't solve your problem , try to show us your code. Regards