为什么我的VBA代码无法选择“下载”才能在此特定网站上下载.xls文件?

时间:2018-11-07 13:54:42

标签: excel vba excel-vba

早上好

我想知道为什么我的代码特别不适用于此站点。我希望能够按“下载”以启动下载.xls文件的过程。但是,事实并非如此,我无法弄清楚。

Public IE As InternetExplorer

Public Sub Main()
  Call InitiateIE
  IE.navigate ("https://www.auction.com/event/details/E-11142/?filter=reset")
  Call LoadIE
  IE.Document.getElementsByClassName("button-root_button_jzfF button-role-default_button_3iu5 button-is-medium_button_3vqd navigation-btn_styles_M1TS download-inventory-btn_styles_3ytA navigation-container-element_styles_3MZ0")(0).Click
  Call LoadIE
  IE.Quit
End Sub

Private Sub InitiateIE()
  Set IE = New InternetExplorer
  IE.Visible = True
End Sub

Private Sub LoadIE(Optional BufferTime As Long = 1000)
  Do While IE.Busy
  Loop
  Do Until IE.Document.ReadyState = "complete" Or IE.Document.ReadyState = "interactive"
  Loop
End Sub

1 个答案:

答案 0 :(得分:2)

顺便说一句,我将使用attribute = value CSS选择器btw

ie.document.querySelector("[data-elm-id='event_details_download_auction_inventory']").Click

Selenium Basic with Chrome:

Option Explicit
Public Sub GetDownload()
'VBE > Tools > References > Selenium Type Library
'Requires latest chrome browser and chrome driver
'Chrome driver containing folder must be on environmental path
'Download selenium https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0
    Dim d As WebDriver
    Set d = New ChromeDriver
    Const URL = "https://www.auction.com/event/details/E-11142/?filter=reset"

    With d
        .Start "Chrome"
        .get URL
        .FindElementByCss("[data-elm-id='event_details_download_auction_inventory']").Click
        .Quit
    End With
End Sub

Other selenium info

Environmental path-驱动程序必须位于该路径上的文件夹中。