getElement does not declare the object (VBA) . IE

Asked

Viewed 418 times

1

I need a little help in a code, VBA interaction in IE, use a page to extract daily reports, and currently this page has undergone a version update, my codes are almost all worked however the code would have to trigger a button to generate an information report, not this worked, tried several ways but unsuccessful, my knowledge is still, I think and simple thing, but I could not finish.

IE page code.

<button title="Iniciar" class="sapMBarChild sapMBtn sapMBtnBase sapMBtnInverted" id="__bar2-btnGo" aria-describedby="__text13" data-sap-ui="__bar2-btnGo"><span class="sapMBtnEmphasized sapMBtnHoverable sapMBtnInner sapMBtnText sapMFocusable sapMIE" id="__bar2-btnGo-inner"><span class="sapMBtnContent" id="__bar2-btnGo-content">Iniciar</span><span class="sapMBtnFocusDiv"></span></span></button>

Cod VBA IE.

'IDENTIFICA A DATA DA REQUISIÇÃO (FINAL) ' ok funcionado

Set objCollection = IE.Document.getElementsByTagName("input")
For Each objElement In objCollection
    If objElement.ID = "__xmlview0--filterDtEndRequest-inner" Then objElement.innerText = Data2
Next objElement                                  '

'IDENTIFICA O BOTÃO PARA EXECUTAR ' Com erro abaixo
'*************************************************************

Set objCollection = IE.Document.getElementsByTagName("button")
For Each objElement In objCollection
    If (objElement.getAttribute("title") = "Iniciar") Then objElement.Click
Next objElement
  • Codigo html&#xA;&#xA;<button title="Iniciar" class="sapMBarChild sapMBtn sapMBtnBase sapMBtnInverted" id="__bar2-btnGo" aria-describedby="__text13" data-sap-ui="__bar2-btnGo"><span class="sapMBtnEmphasized sapMBtnHoverable sapMBtnInner sapMBtnText sapMFocusable sapMIE" id="__bar2-btnGo-Inner"><span class="sapMBtnContent" id="__bar2-btnGo-content">Start</span><span class="sapMBtnFocusDiv"></span></span></span></button> )

  • Please [Edit] the issue with html code

  • danieltakeshi help this format?

1 answer

0

HTML

Adding a JS to the HTML code to check if it was clicked:

function toggleText() {
  var text = document.getElementById("demo");
  if (text.style.display === "none") {
    text.style.display = "block";
  } else {
    text.style.display = "none";
  }
}
<!DOCTYPE html>
<html>

<head>
  <title>Test HTML</title>
</head>

<body>
  <p id='demo' style='display: none'>Hello Teste</p> 
  <button title="Iniciar" class="sapMBarChild sapMBtn sapMBtnBase sapMBtnInverted" id="__bar2-btnGo" aria-describedby="__text13" data-sap-ui="__bar2-btnGo" onclick="toggleText()"><span class="sapMBtnEmphasized sapMBtnHoverable sapMBtnInner sapMBtnText sapMFocusable sapMIE" id="__bar2-btnGo-inner"><span class="sapMBtnContent" id="__bar2-btnGo-content">Iniciar</span><span class="sapMBtnFocusDiv"></span></span>
  </button>
  <script src="script.js"></script>
</body>

</html>

Solution

It is possible to click the button finding the tag of it, which needs to be "button" and if the text is going to start, but using the function Trim(). Because the text has a space and is "Start ".

'Declara função Sleep
#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If

Public Sub ClicarBotao()
    Dim IE As Object
    Dim url As String
    Dim myPoints As String

    url = "http://run.plnkr.co/plunks/TmODUSMPPAxswW7pqyfV/"
    Set IE = CreateObject("InternetExplorer.Application")

    With IE
        .Visible = True
        .navigate url
    End With
    
    EsperaIE IE, 2000

    Dim Doc As HTMLDocument
    Set Doc = IE.document

    For Each elemento In Doc.body.getElementsByTagName("button")
        Debug.Print elemento.Type
        Debug.Print elemento.innerText
        If Trim(elemento.innerText) = "Iniciar" And elemento.Type = "submit" Then
            elemento.Click
        End If
    Next elemento
    Set IE = Nothing

End Sub

Public Sub EsperaIE(IE As Object, Optional time As Long = 250)
    'Código de: https://stackoverflow.com/questions/33808000/run-time-error-91-object-variable-or-with-block-variable-not-set
    Dim i As Long
    Do
        Sleep time
        Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.readyState = 4) & _
                    vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
        i = i + 1
    Loop Until IE.readyState = 4 Or Not IE.Busy
End Sub 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.