web-dev-qa-db-fra.com

Utilisez Excel VBA pour cliquer sur un bouton dans Internet Explorer, lorsque le bouton n'a pas de "nom" associé

J'essaie d'utiliser Excel pour automatiser la valeur entrée dans une feuille de temps. La feuille de temps est sur une page Web. En ce moment, je peux charger la page, entrer mon nom d'utilisateur et mon mot de passe, puis saisir la feuille de temps par elle-même. Voir le code ci-dessous.

À ce stade, je dois cliquer sur un bouton pour ouvrir les sous-formulaires. Je ne peux pas savoir à l'avance combien de sous-formulaires il y aura à ouvrir. Je sais comment cliquer sur un bouton quand il a un "nom". Mais dans ce cas, il n'y en a pas. Donc, mon code mis à jour ci-dessous utilise une boucle pour ouvrir tous les autres sous-formulaires. Ça marche la première fois, mais quand je recommence

Quelqu'un pourrait-il m'indiquer comment déterminer combien de ces boutons il y a dans la page et comment cliquer sur chacun? Après avoir placé le code que j'ai jusqu'à présent et en dessous, le code HTML de la page avec laquelle j'ai besoin d'interagir.

Private Sub time_sheet_filling()

    Dim I As Long
    Dim IE As Object
    Dim doc As Object
    Dim objElement As Object
    Dim objCollection As Object

    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True

    ' Send the form data To URL As POST binary request
    IE.navigate "http://timesheet.cccc.ca/timesheet/"

    ' Wait while IE loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

        'Load the logon page
    Set objCollection = IE.Document.getElementsByTagName("input")
    I = 0
    While I < objCollection.Length
        If objCollection(I).Name = "txtUserName" Then
            ' Set text to enter
            objCollection(I).Value = "6666"
        End If
        If objCollection(I).Name = "txtPwd" Then
            ' Set text for password
            objCollection(I).Value = "password"
        End If
        If objCollection(I).Type = "submit" And objCollection(I).Name = "btnSubmit" Then ' submit button clicking
            Set objElement = objCollection(I)
        End If
        I = I + 1
    Wend

    objElement.Click    ' click button to load the form

    ' Wait while IE re-loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    ' Show IE
    IE.Visible = True  
Dim links, link
Dim n, j
Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
n = links.Length  
For j = 0 To n - 1 Step 2
    links(j).Click
'I have some operations to be done will post another question for this
IE.Document.getElementById"DetailToolbar1_lnkBtnSave").Click              'save
IE.Document.getElementById"DetailToolbar1_lnkBtnCancel").Click            'close
Next



End Sub  

Donc, l'extrait du code html est ci-dessous. J'essaie de cliquer sur le bouton codé dans la dernière ligne du code html ci-dessous

<table width="984" class="Grid" id="dgTime" border="1" rules="all" cellspacing="0">
  <tbody>
    <tr class="GridHeader">
    </tr>
    <tr class="GridItem">
    </tr>
    <tr class="GridItem">
      <td class="GridButtonColumn">
        <a href="javascript:__doPostBack('dgTime$_ctl2$_ctl0','')">
          <img src="images/toolbar/b_edit.gif">
        </a>
      </td  

Tx Tim pour les réponses. Maintenant, je peux sélectionner le premier bouton de sous-formulaire pour l'ouvrir.

links(j).click   'j = 0 

Je l'enregistre ensuite, ferme et reviens au formulaire principal. Mais quand j'essaye de faire

links(j).click   'j = 2 this time

la deuxième fois que j'obtiens une erreur d'exécution 70: autorisation refusée. Une aide plus aimable sera tellement appréciée. Cordialement

4
excelguigui11

Avec l'aimable aide de Tim Williams, j'ai finalement trouvé les derniers détails qui manquaient. Voici le code final ci-dessous.

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
4
excelguigui11

Sélecteur CSS:

Utilisez un sélecteur CSS de img[src='images/toolbar/b_edit.gif']

Cela dit sélectionner les éléments avec la balise img avec l'attribut src ayant la valeur de 'images/toolbar/b_edit.gif'


Requête CSS:

CSS query


VBA:

Vous pouvez appliquer le sélecteur avec le .querySelector méthode de document.

IE.document.querySelector("img[src='images/toolbar/b_edit.gif']").Click
2
QHarr
IE.Document.getElementById("dgTime").getElementsByTagName("a")(0).Click

EDIT: pour parcourir la collection (les éléments doivent apparaître dans le même ordre que dans le document source)

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
2
Tim Williams