网络抓取:如何使用VBA从网页的下拉列表中选择值(选项)

时间:2020-10-29 04:05:14

标签: vba parsing dropdown frame

在一个网站上,我能够登录,然后试图从下拉菜单中选择一个值,但找不到包含该菜单的任何对象。

值得关注的是,该页面似乎正在使用三个框架的窗口-下拉菜单位于其中一个窗口上。

<label for="edit-multiple-form">...</label>
<select id="edit-multiple-form" name="NNN" class="form-select form-control required ajax-processed"> == $0
   <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")

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

我能够抓取下拉菜单对象,但在@@仍然出现错误 怎么了 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

 ( import 'dart:js';).

这应该返回 select HTML元素。然后,您可以进一步缩小范围:

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

因此,您可以遍历(对于i = 0到elemOption.length-1)。请注意,getElementById中没有 s ,因为它始终仅返回1个项目。另外,getElementsByClass不是有效的方法,请改用getElementsByClassName。 列出了可用方法的列表here