vba 使用VBA操作网页上的单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15559408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using VBA to operate radio buttons on a web page
提问by user1563370
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:
我是一名 VBA 新手,试图在网络上操作单选按钮,但运气不佳。我已经用谷歌搜索了几个小时,发现了大量的代码片段,我一直试图修改这些代码片段来完成这项工作,但没有任何成功。它必须是以下的一些变体:
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.
单选按钮有两个选项(用 A 导出,或用 B 导出)。'Export with A' 是自动选择的,我显然需要选择另一个。根据 HTML,按钮的名称是相同的,并且它似乎位于现有页面上弹出的表单上。
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
将 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
回答by user1563370
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!
谢谢大家的帮助!
回答by reaz
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:
最简单的方法是在服务器端使用该代码,因为 VBScript 在浏览器类型、html5 ...等方面存在一些问题这里是在服务器端使用单选按钮的示例:
default.aspx
默认.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
如果这不能解决您的问题,请尝试向我们展示您的代码。问候