【问题标题】:Unable to select the drop down list while web scraping网页抓取时无法选择下拉列表
【发布时间】:2021-07-16 18:11:58
【问题描述】:

我正在尝试使用 Excel VBA macros 抓取网站。但是,我无法使用以下代码选择drop down 列表:

Public Sub Public_Data_pulled()
  Application.Calculation = xlCalculationAutomatic
  Dim html As HTMLDocument, hTable As HTMLTable

  Set html = New HTMLDocument
  
  With CreateObject("MSXML2.XMLHTTP")
    .Open "GET", "https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html", False
    .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    .send
    html.body.innerHTML = .responseText
  End With

  Application.ScreenUpdating = False

  Set hTable = html.getElementById("select-demo")
  Title.selectedIndex = 5

  Application.ScreenUpdating = True

End Sub

【问题讨论】:

  • 通过id获取元素后hTable的值是多少?
  • Title 在表达式Title.selectedIndex = 5 中来自哪里?
  • 我不确定你想通过使用 xmlhttp 执行select 之类的交互来完成什么。

标签: excel vba web-scraping


【解决方案1】:

这是我用来从下拉列表中获取的代码的一部分。

Option Explicit

Public MyElement As Selenium.WebElement

Sub Fill_Report(report As String)

    SearchFor = "select-demo"
    If Not SearchElementdByID(SearchFor) Then GoTo errHandler
    MyElement.Click
       
    ' Select
    MyElement.AsSelect.SelectByText (report)
    
    Exit Sub
    
errHandler:
    Debug.Print Now() & "; " & Err.Number&; "; " & Err.Description
    End
End Sub

Function SearchElementdByID(ByVal Search) As Boolean
    Dim FindBy As New Selenium.By
    
    Set MyElement = MyBrowser.FindElementById(Search)
    MyElement.WaitDisplayed
    
    SearchElementdByID = MyElement.IsDisplayed
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    相关资源
    最近更新 更多