excel vba IE点击一个按钮

时间:2014-03-26 18:47:57

标签: excel vba internet-explorer

通过Excel和VBA,我打开page.

我想点击按钮'展开'通过vba,但我收到一个错误。

我尝试使用下面列出的两个VBA命令

Doc.getElementsByClassName("dhl-btn-main collapse-btn-expand-all")(0).Click
Doc.getElementsByClassName("dhl-btn-main collapse-btn-expand-all").Click

我得到的错误是 运行时错误'':对象不支持此属性或方法。

1 个答案:

答案 0 :(得分:2)

假设" Doc"是你对IE的引用试试这个:

 Set ElementCol = Doc.document.getElementsByClassName("dhl-btn-main collapse-btn-expand-all")

 For Each btnInput In ElementCol
    btnInput.Click
 Next btnInput

工作示例

Private Sub IE_Expand()
    Dim i As Long
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object

    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")

    'IE.Visible = False

    IE.Navigate "https://dhli.dhl.com/dhli-client/shipmentair;jsessionid=q3tzTzyLcL7JkxkNQ4nv7Jtrpzk1glylCyJ7vJzT27h2xBG5zXSm!599496067?0&shipmentId=151218573&accountGroup"

    ' Wait while IE loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    IE.Visible = True

    Set ElementCol = IE.document.getElementsByClassName("dhl-btn-main collapse-btn-expand-all")

    ElementCol.Item(0).Click

    ' Clean up
    Set IE = Nothing
    Set objElement = Nothing
    Set objCollection = Nothing

    Application.StatusBar = ""
End Sub