繁体   English   中英

如何在网页上找到一个按钮

[英]How to find a button on webpage

我正在尝试在网页上执行多项操作以帮助用户设置他们的偏好。 我正在使用 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 的标记名称进行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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM