【问题标题】:Loop through dropdown browsermenu with VBA使用 VBA 循环浏览下拉浏览器菜单
【发布时间】:2021-08-27 13:45:12
【问题描述】:

我需要遍历特定网站的下拉列表并下载每个条目的最新文件。我设法打开网站并单击“提交”按钮,但我找不到循环遍历所有下拉条目的解决方案。

Dim reportnr As String
Dim internetadress As String
Dim btn As Object
Dim IE As SHDocVw.InternetExplorer

reportnr = 10
                         
                                                      
                            
 internetadress = adress & reportnr
    
                    Set IE = CreateObject('InternetExplorer.Application')
                            IE.Visible = True
                            IE.navigate internetadress
                            Do While IE.Busy
                            Application.Wait DateAdd("s", 2, Now)
                            Loop
 

While IE.Busy
DoEvents
Wend

    For Each btn In IE.document.getElementsByClassName("btn btn-primary")

        If btn.name = "submit" Then
            btn.Click
        End If

    Next btn

网站上关于下面下拉菜单的代码。

我尝试了几种方法,但我会出错或什么也没有发生。我尝试的最后一件事是:

Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub SelectQuantity()
    Dim ie As New InternetExplorer, numberOfOptions As Long, i As Long

    With ie
        .Visible = True
        .Navigate2 "https://www.amazon.com/belif-True-Cream-Aqua-Korean/dp/B00H4GOAZO/ref=pd_cart_crc_cko_cp_2_2/139-8277217-3794320?_encoding=UTF8&pd_rd_i=B00H4GOAZO&pd_rd_r=e154e278-8a11-4ab0-8173-5d0dbaff1938&pd_rd_w=Hm8FW&pd_rd_wg=Hpv4X&pf_rd_p=eff166ab-25d2-4f2c-a51d-0a0e86061f9d&pf_rd_r=EVT26E6K7CV8T1QMTY7H&psc=1&refRID=EVT26E6K7CV8T1QMTY7H"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            numberOfOptions = .querySelectorAll("#quantity option").Length 'gather option tag element children of parent select element with id quantity
            For i = 1 To numberOfOptions
                .querySelector("#quantity [value='" & i & "']").Selected = True
                ActiveSheet.Cells(i, 1) = i
            Next
            Stop
        End With
    End With
End Sub

来源:VBA loop through dropdown elements from web page and download to excel sheet

提前感谢您的帮助。

【问题讨论】:

  • 我们看不到 select 元素的相关 html。此外,通过edit 使用 sn-p 工具插入 html 而不是发布图像。此外,当您循环到 .Length 而不是 .Length -1 时,您的代码将失败。尴尬的是,原来的代码是我的错。我已将引用链接的那部分编辑为正确的。道歉。
  • 网站是内部的吗?如果没有,您可以提供网站的链接,以便我们进行测试,看看如何提供帮助。如果不能提供链接,请提供下拉列表的详细html代码,而不是图片。
  • 好的,对不起,我下次会这样做。提前感谢您的帮助。该网站的链接如下:theice.com/marketdata/reports/10。我们需要每天在下拉菜单中获取文件。

标签: html vba object internet-explorer web-scraping


【解决方案1】:

在您提供的网站上,您可以尝试使用以下代码循环下拉列表以选择您想要的选项:

Set lists = IE.document.getElementsByClassName("active-result")  'get all the options li elements

For Each liElement In lists
    If liElement.innerText = "something" Then
         liElement.setAttribute "class", "active-result result-selected"
    End If
Next liElement

我发现选择的选项有不同的类,即active-result result-selected,所以我想也许你可以改变类来选择选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 2016-03-27
    相关资源
    最近更新 更多