使用VBA操作网页上的单选按钮 [英] Using VBA to operate radio buttons on a web page

查看:586
本文介绍了使用VBA操作网页上的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VBA新秀,正在尝试操作网络上的单选按钮,但运气不佳.我已经搜查了几个小时,并且发现了数不胜数的代码片段,我一直在尝试修改这些代码片段来完成这项工作,但都没有成功.必不可少的是:

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

单选按钮是两个选项(用A导出或用B导出).自动选择导出为A",显然我需要选择另一个.根据HTML,按钮的名称都是相同的,并且看起来好像是在现有页面上弹出的表单上.

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!

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

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:

将IE视作对象 昏暗的ieDoc作为对象 昏暗的锚作为对象 昏暗的ieAnchors作为对象

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

推荐答案

我能够将其与以下代码一起使用:

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

谢谢大家的帮助!

这篇关于使用VBA操作网页上的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆