How to call onClick with VBA in HTML code

左心房为你撑大大i 提交于 2020-03-04 07:18:28

问题


I'm from Brazil, and want to know how I can call onClick in a HTML page.

Using Google Chrome, I got this code:

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

And 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

So, I have no idea how do a can click on this code, In the web site this is a Image and the firts 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.

Does anybody can Help me???? And sorry about any english problem.


回答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"



回答2:


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:


VBA:

You apply the selector via the querySelector method of document.

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


来源:https://stackoverflow.com/questions/32122510/how-to-call-onclick-with-vba-in-html-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!