excel vba IE 单击按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22670379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:30:21  来源:igfitidea点击:

excel vba IE clicking a button

excelvbainternet-explorer

提问by user2543622

Through Excel and VBA, i am opening a page.

通过 Excel 和 VBA,我打开了一个页面。

I want to click on the button 'Expand' through vba but I am getting an error.

我想通过 vba 单击“展开”按钮,但出现错误。

I tried using both VBA commands listed below

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

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

The error that I get is Run time error '438': Object doesn't support this property or method.

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

回答by Portland Runner

Assuming that "Doc" is your reference to IE try this:

假设“Doc”是您对 IE 的引用,请尝试以下操作:

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

 For Each btnInput In ElementCol
    btnInput.Click
 Next btnInput


Working Example:

工作示例

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