【问题标题】:Call Javascript directly for 'click' button直接为“点击”按钮调用 Javascript
【发布时间】:2019-10-19 15:51:43
【问题描述】:

我需要点击没有 id 和 name 的“按钮” 这是对 javascipt 的一个 href ..

【问题讨论】:

  • 试试 execScript("callSearch();")?不确定它会起作用,只是看起来像正确的语法。顺便说一句,那是一些可怕的 html。
  • 有网址可以使用吗?
  • 嗨 Lorne,确实太可怕了……嗨 QHarr。抱歉,无法共享 URL,因为它是 Intranet。但这几乎是该页面的整个 html 代码
  • 在“try1”中a.click真的执行了吗?
  • @蒂姆。能够找到元素,执行点击,但不能响应

标签: excel vba internet-explorer web-scraping


【解决方案1】:

尝试使用以下代码,似乎当我们点击链接时,它会触发 JavaScript 函数:

Sub Main()
    'we define the essential variables

    Dim IE As Object, Data As Object
    Dim ticket As String


    Set IE = CreateObject("InternetExplorer.Application")

    With IE
        .Visible = True
        .navigate ("<the website url>")

        While IE.ReadyState <> 4
            DoEvents
        Wend

        Set Data = IE.Document.getElementsByClassName("appbut")

        Debug.Print Data.Length

        If Len(Data) > 0 Then
            For Each ee In Data

                Set link = ee.getElementsbyTagName("a")(0)
                ' it is better to check whether we could find the a tag.
                'check whether we could get the innertext.
                Debug.Print link.InnerText

                If link.InnerText Like "Cancel" Then

                    'click the link.
                    link.Click

                End If
            Next ee

        End If
    End With
    Set IE = Nothing
End Sub

网页中的代码:

<script>
    function callLookup() {
        alert("Party Search");
    };
    function callCancel() {
        alert("Cancel");
    };
    function callSearch() {
        alert("Search");
    }
</script>
<table  >
    <tr valign="bottom">
        <td colspan="5">
            <table  >
                <tbody>
                    <tr>
                        <td class="butspace"></td>
                        <td class="butspace"></td>
                        <td class="butspace"></td>
                        <td class="butspace"></td>
                        <td class="butspace"></td>
                        <td class="butspace"></td>
                        <td class="butspace"></td>

                        <td class="butspace"></td>
                        <td class="appbut"><a href="javascript:callLookup();">Party Search</a></td>
                    </tr>
                    <tr>
                        <td class="appbut"><a href="javascript:callCancel()">Cancel</a></td>
                        <td class="butspace"></td>
                        <td class="appbut"><a href="javascript:callSearch();">Search</a></td>
                        <td></td>
                        <td></td>
                    </tr>
                </tbody>
            </table>
        </td>
    </tr>

</table>

this 这样的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    • 2014-03-25
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多