首页 文章

使用VBA操作网页上的单选按钮

提问于
浏览
1

我是一名试图在网络上操作单选按钮的VBA新秀,但我没有太多运气 . 我一直在谷歌搜索几个小时,并找到了大量的代码片段,我一直试图修改,以完成这项工作,但没有任何成功 . 必须是它的一些变化:

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

单选按钮有两个选项(使用A导出或使用B导出) . “自动导出”会自动选中,我显然需要选择另一个 . 根据HTML,按钮的名称都是相同的,它似乎位于现有页面上弹出的表单上 .

如何选择第二个按钮?我确定我遗漏了一些重要信息,所以如果我需要提供其他任何信息,请告诉我,我很乐意 . 感谢您的帮助!

编辑:有一些代码,我相信这是一个弹出的子窗口(它不是一个单独的窗口,而是在现有窗口中的窗格):

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

然后该框的代码如下所示:

<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>
<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>

到目前为止我的代码(打开页面,单击该页面上的链接,使用单选按钮创建窗格:

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

2 回答

  • 3

    我能够使用以下代码使用它:

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

    感谢大家的帮助!

  • 0

    最简单的方法是在服务器端使用该代码,因为VBScript在浏览器类型方面存在一些问题,html5 ......等以下是在服务器端使用radiobuttons的示例:

    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
    <input id="Radio2" type="radio" value="ExB" name="radbtnEx" runat="server" />Export with B
    </form> </body> </html>

    如果这不能解决您的问题,请尝试向我们展示您的代码 . 问候

相关问题