我在正确使用语法从网页的下拉列表中选择项目时遇到麻烦。选择性别。我认为这与使用javascript有关。当我在下拉菜单中检查元素时,我得到以下信息:
<table width="100%" style="z-index: auto;" cellspacing="0" cellpadding="0" oldz="auto"><tbody style="z-index: auto;" oldz="auto">
<tr style="z-index: auto;" oldz="auto">
<td style="width: 100%; z-index: auto;" oldz="auto">
<div class="FBox" style="width: auto; position: relative; z-index: auto;" oldz="auto" outerdiv="Y"><input name="VX_Gender" id="VX_Gender" type="hidden" value="M">
<div class="EditDiv EditDropDown" id="VX_Gender_ext" role="textbox" contenteditable="true" spellcheck="false" ondrop="return false" origval="Male" hig="1" max="-1" strictselection="Y" focstyle="EditDropDownFoc" isfilter="Y" filterbox="Y" val2="Male" LastTouchType="Y">Male</div>
<script>initDiv('VX_Gender');</script>
</div>
</td>
<td style="white-space: nowrap;"></td>
</tr>
</tbody>
</table>
当我对下拉菜单项执行相同操作时,我得到:
<table width="100%" style="z-index: auto;" cellspacing="0" cellpadding="0" oldz="auto">
<div class="filtermenu" style="left: 271.76px; top: 252.5px; width: 122px; height: auto; margin-top: 0px; display: none; z-index: 1698; -ms-overflow-y: auto; max-height: 67px;" onclick="return filterPopupClick( event );" hovermenu="Y" shrink="Y" unselectable="on" lastfilter="Male">
<div class="bcrow rsel" onmouseover="return fRowHov(this);" value="F" origva="Female" hc="#FFFFFF" fkey="F" frow="Y" fIdx="2">
Fe
<span class="fsr">male</span>
</div>
<div class="bcrow" onmouseover="return fRowHov(this);" value="I" origva="Indeterminate" hc="#FFFFFF" fkey="I" frow="Y" fIdx="-1">Indeterminate</div>
<div class="bcrow" onmouseover="return fRowHov(this);" value="M" origva="Male" hc="#FFFFFF" fkey="M" frow="Y" fIdx="0"><span class="fsr">Male</span></div>
<div class="bcrow" onmouseover="return fRowHov(this);" value="U" origva="Unborn" hc="#FFFFFF" fkey="U" frow="Y" fIdx="-1">Unborn</div>
</div>
我已经在代码中尝试了多种格式,但是似乎都没有用。任何帮助将不胜感激。
Sub gender()
Dim URL, PER_ID As String
Dim objShell, ie As Object
Dim ele As IHTMLElement
'Determine if a specific instance of IE is already open.
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next 'Sometimes more web pages are counted than are open
URL = objShell.Windows(x).document.Location
If URL = "https://protocolshef.syhapp.com:51020/web/faorc.htm" Then 'Identify the existing web page
Set ie = objShell.Windows(x)
Exit For
Else
End If
Next
For Each ele In ie.document.getElementsByTagName("div")
If ele.ID = "VX_Gender_ext" Then
ele.innerText = "Female"
ele.Val2 = "Female"
Call ie.document.parentWindow.execScript("initDiv('VX_Gender');", "JavaScript")
End If
Next
For Each ele In ie.document.getElementsByName("VX_Gender")
ele.Value = "F"
ele.origval = "F"
Next
For Each ele In ie.document.getElementsByTagName("div")
If ele.ID = "VX_Gender_ext" Then
ele.innerText = "Female"
ele.Val2 = "Female"
Call ie.document.parentWindow.execScript("initDiv('VX_Gender');", "JavaScript")
End If
Next
For Each ele In ie.document.getElementsByName("VX_Gender")
ele.Type = "text"
ele.Value = "F"
ele.origval = "F"
Next
End Sub
对QHarr的建议进行代码测试,不幸的是该建议不起作用。
Sub gender()
Dim URL, PER_ID As String
Dim objShell, ie As Object
Dim x, IE_count As Long
Dim ele As IHTMLElement
'Determine if a specific instance of IE is already open.
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next 'Sometimes more web pages are counted than are open
URL = objShell.Windows(x).document.Location
If URL = "https://protocolshef.syhapp.com:51020/web/faorc.htm" Then 'Identify the existing web page
Set ie = objShell.Windows(x)
Exit For
Else
End If
Next
Set ele = ie.document.querySelectorAll("div[frow=Y]")
ele.Item(1).Selected = True
End Sub
答案 0 :(得分:0)
经过大量调试后,解决方案如下:
ie.document.getElementsByTagName("form")(0).querySelector("div[fkey=F]").Click
需要导航一个表单,然后导航div[fkey=F]
的CSS选择器组合以定位有问题的元素。
选择器组合查找具有属性div
且值为fkey
的{{1}}元素。