首页 > 解决方案 > 使用 VBA 单击按钮元素

问题描述

我相信我已经很接近了,但正在努力寻找并点击本网站上的正确元素。我已经让 VBA 为我登录并移动页面,但这个现在让我难过。我在用

以下是我检查要单击的按钮时的 HTML 选择:

[1]:https://i.stack.imgur.com/HIWEP.png

我一直在尝试使用一些方法,但我觉得我应该能够使用以下方法来获得它(我有一条评论指出应该在哪里):

Sub Login()

    Const Url$ = "examplewebsite.com"

    Dim HTMLDoc As HTMLDocument
    Dim oHTML_Element As IHTMLElement


    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")

    With ie

        .Navigate Url
        .Visible = True
    
        Do While ie.Busy Or ie.ReadyState < 4
            DoEvents
        Loop
  
    
        Set HTMLDoc = ie.Document
        
        
        Dim Login As Object
        Dim Password As Object
        Dim LoginButton As Object
    
        For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
            If oHTML_Element.Name Like "txt_1*" Then Set Login = oHTML_Element
            If oHTML_Element.Name Like "txt_2*" Then Set Password = oHTML_Element
            If oHTML_Element.Name Like "btnLogin*" Then Set LoginButton = oHTML_Element
        Next



        Login.Value = ""
        Password.Value = ""
    
        LoginButton.Click
    
    
        Do While ie.Busy Or ie.ReadyState < 4
        DoEvents
        Loop
        
        Call HTMLDoc.parentWindow.execScript("LoadContent('Report1017','ReportsList/ReportList.aspx?ReportParam=1017')", "JavaScript")
       
       
       Do While ie.Busy Or ie.ReadyState < 4
        DoEvents
        Loop
        
       'This is where I am stumped but think something like this should work:
       HTMLDoc.getElementById("dlReportList_ctl00_hdnReportId").Click
        
        
         
    End With

End Sub

标签: excelvba

解决方案


推荐阅读