1

How can I call onClick in a HTML page?

Using Google, I got this code:

<img src="Imagens/lupa.gif" alt="Pesquisar Pasta" class="img" onclick="localizar();">

I'm using this VBA code:

Sub FazerLoginSite()

Dim IE As Object
Set IE = CreateObject("InternetExplorer.application")
With IE
    .Visible = True
    .Navigate ("https://webseal.itau/j146/pjuridico/")
    While .Busy Or .ReadyState <> 4: DoEvents: Wend
    On Error Resume Next
    .document.getElementById("username").Focus
    .document.getElementById("username").Value = "987268556"

    .document.getElementById("password").Focus
    .document.getElementById("password").Value = ""

    .document.All("button1").Click
    On Error GoTo 0
    While .Busy Or .ReadyState <> 4: DoEvents: Wend

    While .Busy Or .ReadyState <> 4: DoEvents: Wend
    .document.getElementById("pasta").Focus
    .document.getElementById("pasta").Value = "140200586125"
    While .Busy Or .ReadyState <> 4: DoEvents: Wend
   
    While .Busy Or .ReadyState <> 4: DoEvents: Wend
    Set allInputs = IE.document.getElementsByTagName("input")
    For Each element In allInputs
        If element.getAttribute("src") = "Imagens/lupa.gif" Then
            element.invokemember ("OnClick")
            Exit For
        End If
    Next element
    While .Busy Or .ReadyState <> 4: DoEvents: Wend

    Debug.Print .LocationURL
End With

End Sub

How can I click on this code? In the web site this is an Image and the first part of the code I set my login and password, the I put a number to search, here called "Pasta", and finally I'm trying to click on this image.

2 Answers 2

1

There shouldn't be any need to find the element. You can just call the same JavaScript function that onClick is calling using the execScript() function:

IE.document.parentWindow.execScript "localizar();", "javascript"
Sign up to request clarification or add additional context in comments.

Comments

1

CSS selector:

Use a CSS selector to target the element of:

img[src='Imagens/lupa.gif']

This says element with img tag, having attribute src with value 'Imagens/lupa.gif'.


CSS query:

CSS query


VBA:

You apply the selector via the querySelector method of document.

ie.document.querySelector("img[src='Imagens/lupa.gif']").Click

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.