首页 > 解决方案 > Excel vba从网页上的下拉列表中选择项目

问题描述

我无法正确获取从网页下拉列表中选择项目的语法。就是选择性别。我认为这与使用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

标签: javascripthtmlvbaweb-scraping

解决方案


经过多次调试,解决方案如下:

ie.document.getElementsByTagName("form")(0).querySelector("div[fkey=F]").Click 

需要导航一个表单,然后使用 CSS 选择器组合div[fkey=F]来定位相关元素。

选择器组合查找属性值为的div元素。fkeyF


推荐阅读