javascript - 如何在网页上找到按钮

标签 javascript html excel vba

我正在尝试在网页上执行多项操作以帮助用户设置他们的首选项。我正在使用 Excel VBA,因为它是公司自动化任务的唯一方法。

首先,他们必须登录,这是我能够执行的操作。然后,我浏览到相应的页面。

问题发生在这里:如果我用 Internet Explorer 右键单击​​页面并显示源代码,则只显示页面的一部分。另外,根据我执行此操作的区域,源代码不同,它不是一个整体。
如果我使用 DOM Explorer,情况就不同了。显示完整的页面代码,我可以指向我想要单击的按钮。

按钮代码:

<a href="javascript:somewords('somelink',var1,var2,false)">Click here !</a>

我的代码:

Sub Connect_to_system()

    Set IE = New InternetExplorerMedium
    Dim IEDoc As HTMLDocument
    Dim htmlTagCol As IHTMLElementCollection
    Dim Generic As HTMLGenericElement
    
    With IE
        .Visible = True
        .navigate "http://example.com/login?username=johndoe&password=123"

        Do Until .readyState = 4
            DoEvents
        Loop
        
        .navigate "http://example.com/somepage.jsp?reference123"
        
        Do Until .readyState = 4
            DoEvents
        Loop
        
        Application.Wait (Now + TimeValue("0:00:10"))
        
        Set tags = IE.document.getElementsByTagName("a")
        For Each tagx In tags
            If tagx.href = "javascript:somewords('somelink',var1,var2,false)" Then
                tagx.Click
            End If
        Next
    
    End With
End Sub

我的代码循环了 3 次(所以我知道它检测到标签)但没有一个符合我的条件。所以它脱离了循环,什么也没有发生。
我试图查看检测到的标签,似乎只有页面顶部的功能区被识别/加载。
即使使用 InnerText 函数,这些方法也不起作用。

最佳答案

使用您提供的最少 html 代码,这将是必需的。

您可以使用 innerHTML 将元素上的字符串值与您的标签名称 a 进行比较。

Sub ieBusy(ie As InternetExplorerMedium)

    Do While ie.Busy Or ie.readyState < 4
        DoEvents
    Loop

End Sub

Sub Connect_to_system()

    Dim ie As New InternetExplorerMedium, aElemColl As Object, aElem As Object

    With ie

        .Visible = True
        .navigate "http://example.com/login?username=johndoe&password=123"
        ieBusy ie
        .navigate "http://example.com/somepage.jsp?reference123"
        ieBusy ie
        Set aElemColl = .document.getElementsByTagName("a")

    End With

    For Each aElem In aElemColl

        If aElem.innerHTML Like "*javascript:somewords('somelink',var1,var2,false)*" Then
            aElem.Click
            Exit For
        End If

    Next

End Sub

关于javascript - 如何在网页上找到按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48449435/

相关文章:

javascript - 我可以在 twitter-bootstrap popover 数据内容中使用 html 标签吗?

javascript - 如何使用jquery获取最后2位数字

javascript - React native-通过耳机播放自定义声音

javascript - 如何使用 JavaScript 在点击事件中显示工具提示

javascript - 联系表格 7 ajax 目标跟踪

html - 页面加载ajax内容后不出现滚动条

javascript - 为什么我的图像从 Jquery 反弹效果中移动?

vba - Excel VBA - 创建新工作表的宏导致奇怪的故障

excel - 获取文件路径(以文件夹结尾)

excel - Excel 2007 的 Mime 类型