Use Excel VBA to click on a button in Internet Explorer, when the button has no “name” associated

前端 未结 3 1320
[愿得一人]
[愿得一人] 2020-11-30 06:54

I\'m trying to use excel to automate the value entering in a time sheet. The time sheet is on a web page. Right now I\'m able to load the page, enter my username and passwo

相关标签:
3条回答
  • 2020-11-30 07:37

    CSS selector:

    Use a CSS selector of img[src='images/toolbar/b_edit.gif']

    This says select element(s) with img tag with attribute src having value of 'images/toolbar/b_edit.gif'


    CSS query:


    VBA:

    You can apply the selector with the .querySelector method of document.

    IE.document.querySelector("img[src='images/toolbar/b_edit.gif']").Click
    
    0 讨论(0)
  • 2020-11-30 07:44

    With the kind help from Tim Williams, I finally figured out the last détails that were missing. Here's the final code below.

    Private Sub Open_multiple_sub_pages_from_main_page()
    
    
    Dim i As Long
    Dim IE As Object
    Dim Doc As Object
    Dim objElement As Object
    Dim objCollection As Object
    Dim buttonCollection As Object
    Dim valeur_heure As Object
    
    
    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
    ' You can uncoment Next line To see form results
    IE.Visible = True
    
    ' Send the form data To URL As POST binary request
    IE.navigate "http://webpage.com/"
    
    ' Wait while IE loading...
    While IE.Busy
            DoEvents
    Wend
    
    
    Set objCollection = IE.Document.getElementsByTagName("input")
    
    i = 0
    While i < objCollection.Length
        If objCollection(i).Name = "txtUserName" Then
            ' Set text for search
            objCollection(i).Value = "1234"
        End If
        If objCollection(i).Name = "txtPwd" Then
            ' Set text for search
            objCollection(i).Value = "password"
        End If
    
        If objCollection(i).Type = "submit" And objCollection(i).Name = "btnSubmit" Then ' submit button if found and set
            Set objElement = objCollection(i)
        End If
        i = i + 1
    Wend
    objElement.Click    ' click button to load page
    
    ' Wait while IE re-loading...
    While IE.Busy
            DoEvents
    Wend
    
    ' Show IE
    IE.Visible = True
    Set Doc = IE.Document
    
    Dim links, link
    
    Dim j As Integer                                                                    'variable to count items
    j = 0
    Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
    n = links.Length
    While j <= n                                    'loop to go thru all "a" item so it loads next page
        links(j).Click
        While IE.Busy
            DoEvents
        Wend
        '-------------Do stuff here:  copy field value and paste in excel sheet.  Will post another question for this------------------------
        IE.Document.getElementById("DetailToolbar1_lnkBtnSave").Click              'save
        Do While IE.Busy
            Application.Wait DateAdd("s", 1, Now)                                   'wait
        Loop
        IE.Document.getElementById("DetailToolbar1_lnkBtnCancel").Click            'close
        Do While IE.Busy
            Application.Wait DateAdd("s", 1, Now)                                   'wait
        Loop
        Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
        j = j + 2
    Wend    
    End Sub
    
    0 讨论(0)
  • 2020-11-30 07:48
    IE.Document.getElementById("dgTime").getElementsByTagName("a")(0).Click
    

    EDIT: to loop through the collection (items should appear in the same order as they are in the source document)

    Dim links, link 
    
    Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
    
    'For Each loop
    For Each link in links
        link.Click
    Next link
    
    'For Next loop
    Dim n, i
    n = links.length
    For i = 0 to n-1 Step 2
        links(i).click
    Next I
    
    0 讨论(0)
提交回复
热议问题