我正在尝试获取我的一些代码来更改内部网页上的单选按钮,但是,页面编写方式我想要的元素没有特定的ID。以下是代码示例:
<div align="center" id="divControls">
<form id="frmSearch" name="frmSearch" method="POST">
<table cellpadding="0" cellspacing="0" class="exec_tbl align_vc">
<tr>
<td class="exec_ltxt"><input type="radio" name="search" value=1 checked=checked onclick="collapse('submit');searchOptions();" /> Agent Search</td>
<td class="exec_ltxt"><input type="radio" name="search" value=2 onclick="expand('submit');searchOptions();" /> Team Search</td>
<td class="exec_ltxt"><input type="radio" name="search" value=3 onclick="expand('submit');searchOptions();" /> Manager Search</td>
<td class="exec_ltxt"><input type="radio" name="search" id='searchlob' value=4 onclick="expand('submit');searchOptions();" /> LoB Search</td>
</tr>
</table>
</form>
</div>
我只需要点击单选按钮值= 2(团队搜索)。
我试图使用的VBA是:
Sub Attendance_RoundedRectangle2_Click()
sCSVLink = "webpage"
sfile = "csvexport.php"
ssheet = "Sheet10"
Dim appIE As InternetExplorerMedium
Dim Obj As Object
On Error Resume Next
Application.ScreenUpdating = False
Set appIE = New InternetExplorerMedium
Set ieRadio = IE.document.all
sURL = "webpage"
' Instructes the macro to open IE and navigate to sURL.
With appIE
.Navigate sURL
.Visible = True
Application.Wait Now + TimeValue("00:00:02")
Set HTMLDOC = .document
End With
Application.Wait Now + TimeValue("00:00:02")
appIE.document.all.Item("divControls")(3).Checked = True
appIE.document.getElementByid("divControls").Value = "2"
End Sub
我绝不是专业人士,我想把它拼凑起来,非常感谢我能得到的任何帮助!
更新了HTML
<div align="right"><a href="index.php?logout=1">logout</a></div><h1 class="font_2 & uline & center" style="color:#909090;">Reporting</h1>
<div align="center" id="divControls">
<form id="frmSearch" name="frmSearch" method="POST">
<table cellpadding="0" cellspacing="0" class="exec_tbl align_vc">
<tr>
<td class="exec_ltxt"><input type="radio" name="search" value=1 checked=checked onclick="collapse('submit');searchOptions();" /> Agent Search</td>
<td class="exec_ltxt"><input type="radio" name="search" value=2 onclick="expand('submit');searchOptions();" /> Team Search</td>
<td class="exec_ltxt"><input type="radio" name="search" value=3 onclick="expand('submit');searchOptions();" /> Manager Search</td>
<td class="exec_ltxt"><input type="radio" name="search" id='searchlob' value=4 onclick="expand('submit');searchOptions();" /> LoB Search</td>
</tr>
<tr>
<td class="exec_ltxt" colspan="3"><div id="divSearchOption" align="center" style="display:inline;width:100%;"></div></td>
<td class="exec_ctxt"><input id="submit" type="button" value="Submit" style="display:none;" onclick="submitButton();"></td>
</tr>
<tr>
<td class="exec_rtxt">Start Date:</td>
<td class="exec_ltxt">
<input id="sDate" name="sDate" type="text" size="9" value="2014-01-08" onchange="if (document.getElementById('searchlob').checked != true) { searchOptions(); }" onblur="if (document.getElementById('searchlob').checked != true) { searchOptions(); }" />
<img src="icon_pickdate.gif" id="start_date_button" alt="date picker" />
</td>
<td class="exec_rtxt">End Date:</td>
<td class="exec_ltxt">
<input id="eDate" name="eDate" type="text" size="9" value="2015-01-08" onchange="if (document.getElementById('searchlob').checked != true) { searchOptions(); }" onblur="if (document.getElementById('searchlob').checked != true) { searchOptions(); }" />
<img src="icon_pickdate.gif" id="end_date_button" alt="date picker" />
</td>
</tr>
</table>
<input id="uid" name="uid" type="hidden" value="agent" />
</form>
</div>
<div id="divTeamTable" align="center"></div>
<br/>
<div align="center" id="divAgentData"></div>
我遗漏的唯一信息是标题,其中包含几个不应影响VBA的JavaScript链接。
答案 0 :(得分:0)
您不需要使用Application.Wait暂停正在运行的宏,您可以在宏导航到URL并尝试通过DOM工作之前先让事件发生
在With ... End With块中,在.Visible = True行之前添加:
Do
DoEvents
Loop Until .ReadyState = 4
.Document.getElementsByTagName("input")(1).Checked = True
删除这些代码行,因为它们不再用于任何目的:
Set ieRadio = IE.document.all
Set HTMLDOC = .document
Application.Wait Now + TimeValue("00:00:02")
appIE.document.all.Item("divControls")(3).Checked = True
appIE.document.getElementByid("divControls").Value = "2"
答案 1 :(得分:0)
我认为这可能有所帮助,效率不高,但有效。
set inputObjs=appIE.document.getElementByTag("input")
for each obj in inputObjs
if(obj.value=2 ) then obj.click
next