首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >单击JavaScript按钮

单击JavaScript按钮
EN

Stack Overflow用户
提问于 2015-08-17 21:59:44
回答 1查看 1.7K关注 0票数 0

有人能给我看一下VBA代码来点击这个网页上的“复制到剪贴板”按钮吗?该按钮有一个元素id="ToolTables_example_0",但我不知道如何从Excel执行脚本。我尝试过重新设计我找到的许多不同的VBA代码,但是没有运气。看起来应该这么简单!谢谢!

awphys&lat=30.26678&lon=-97.76905&tz=America/Chicago&display=table

EN

回答 1

Stack Overflow用户

发布于 2015-08-18 01:37:07

下面的代码要求使用VBE的工具►引用命令将Microsoft对象库和Microsoft控件添加到项目中。

以下是点击该按钮的两种方法。

代码语言:javascript
运行
复制
Sub clickIt()
    Dim a As Long, u As String, till As Double
    Dim el As MSHTML.IHTMLElement, ie As New SHDocVw.InternetExplorer

    u = "https://spotwx.com/products/grib_index.php?model=nam_awphys&lat=30.26678&lon=-97.76905&tz=America/Chicago&display=table"

    ie.Silent = True
    ie.Visible = True

    ie.navigate u
    Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
    'wait an extra second or so (this usually isn't necessary but helped with this page)
    till = Timer + 1
    Do While Timer < till: DoEvents: Loop

    'Method 1: Loop through all the A elements looking for the right ID
    For a = 0 To ie.document.getElementsByTagName("a").Length - 1
        With ie.document.getElementsByTagName("a")(a)
            If .ID = "ToolTables_example_0" Then
                Debug.Print "found ToolTables_example_0 in a loop"
                .Click
                'the table data should be on the clipboard
                Exit For
            End If
        End With
    Next a

    'Method 2: Locate the right A element using its ID directly
    On Error Resume Next
    Set el = ie.document.getElementById("ToolTables_example_0")
    On Error GoTo 0

    If Not el Is Nothing Then
        Debug.Print "found ToolTables_example_0 directly"
        el.Click
        'the table data should be on the clipboard
    End If

    ie.Quit
    Set ie = Nothing

End Sub

我无法完全测试这一点,因为我的ie没有安装Adobe (我更愿意保持这种方式)。如果您可以手动打开Internet,导航到该页面,单击该按钮,然后将复制到剪贴板的内容粘贴回Excel工作表,那么我看不出为什么它不工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32060395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档