我是一名 VBA 菜鸟,试图在网络上操作单选按钮,但运气不佳。我已经在谷歌上搜索了几个小时,并且发现了大量的代码片段,我一直在尝试修改这些代码片段来完成这项工作,但没有任何成功。它必须是一些变化:
ie.Document.getElementsByName("name_of_radiobox").Item(0).Checked = True
单选按钮有两个选项(使用 A 导出或使用 B 导出)。'Export with A' 是自动选择的,我显然需要选择另一个。根据 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><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>
到目前为止我的代码(打开页面,单击该页面上的链接,创建带有单选按钮的窗格:
将 IE 调暗为对象 将 ieDoc 调暗为对象 将锚点调暗为对象 将 ieAnchors 调暗为对象
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