Web 抓取:如何使用 VBA 从网页的下拉列表中 select 一个值(选项)

Webscraping: how to select a value (option) from a dropdown list on a webpage using VBA)

在一个网站上,我能够登录,然后我试图从下拉菜单中 select 一个值,但我无法获得任何包含该菜单的对象。

令人担忧的是,该页面似乎使用了三个框架 windows- 下拉菜单位于 windows 之一。

<label for="edit-multiple-form">...</label>
<select id="edit-multiple-form" name="NNN" class="form-select form-control required ajax-processed"> == [=12=]
   <option value selected="selected">- Select -</option>
   <option value="1"> OPTION1 </option>
   <option value="2"> OPTION2 </option>
   <option value="3"> OPTION3 </option>
</select>
::after
</div>

VBA 代码(更新):

Sub LogIn()

' ' ' ' ' 

'when Error
On Error GoTo Err_Clear

' ' ' ' ' 

'New HTML Document for the page
Dim IE3 As HTMLDocument
Set IE3 = IEBrowser.document

Dim elemSelect As IHTMLElement
Dim elemOption As IHTMLElement

Set elmSelect = IE3.getElementById("edit-multiple-wallet-form")
    Debug.Print elemSelect.innerText        
Set elemOption = elemSelect.getElementByTagName("option")  '@@ it gives me an error
If elemOption.Length > 0 Then
    Dim i, text
    For i = 0 To elemOption.lengh - 1
        text = text & elemOption.Item(i).innerText
    Next
    MsgBox "All pre elements text: " & text
End If
 
    ' ' ' ' ' 


Err_Clear:
If Err <> 0 Then 
Err.Clear
Resume Next
End If

End Sub

=========================================== =======================

 'Got a new HTML document after log on the the first page
    Dim IE3 As HTMLDocument
    Set IE3 = IEBrower.document
    
'then tried several methods to get the object using the following commands 
IE3.document.querySelector("option")

IE3.all.NNN.value   'NNN is the name of the dropdown menu object

IE3.getElementsByID("edit-multiple-form)

IE3.getElementsByClass("form-select form-control required ajaz-processed")

=========================================== =============================

我能够获取下拉菜单对象,但在@@ 处仍然出现错误 它有什么问题? 任何帮助将不胜感激。

IE3.getElementById("edit-multiple-form")

这应该 return select HTML 元素。然后你可以进一步缩小范围:

Set elemSelect = IE3.getElementById("edit-multiple-form")
Set elemOption = elemSelect.getElementsByTagName("option")

那么你就可以循环了(对于 i=0 到 elemOption.length-1)。请注意,getElementById 中没有 s,因为它始终正好是 returns 1 项。此外,getElementsByClass 不是有效方法,请改用 getElementsByClassName。 列出了可用方法的列表 here