How to access menu mouseover with VBA

Asked

Viewed 119 times

0

Good afternoon! I’m trying to automate the filling of a form for a website, however, I’m having difficulty entering the menu where the form is. It works like this: I login to the site through a userform, where I inform the user and password and click on "login"... until then ok, I was able to do. After logging in, I need to enter a menu that expands when I hover over and it opens other submenus (+ 2 levels). I need to click on the second level of one of these submenus to view the form and I’m not getting the macro to click. I don’t know if I could explain well, I don’t know much about programming. I’ll leave the VBA code. And if you need any more information, let me know. Note: I found this post but it didn’t work in my case: Access menu mouseover by VBA

Sub Robo_apac_1()

Dim IE As InternetExplorer

'Cria um objeto Internet Explorer
Set IE = New InternetExplorer
    
'Torna o objeto visível
IE.Visible = True

    'Navega ao site do SIGA Saúde
    IE.Navigate "http://siga.saude.prefeitura.sp.gov.br/sms/login.do?method=logoff"
            
    'Identifica se a página já foi totalmente carregada
    While IE.ReadyState <> READYSTATE_COMPLETE
    Wend
     
        'aguardar 3 segundos (se necessário)
        'sng = Timer
        'Do While sng + 3 > Timer
        'Loop
    
    IE.Document.All("j_username").Value = frmlogin.txbLogin
    IE.Document.All("j_password").Value = frmlogin.txbSenha
    IE.Document.All("confirmar").Click

'fehca a tela te login
Unload frmlogin
        
        'aguardar 2 segundos antes de validar o logon
        sng = Timer
        Do While sng + 2 > Timer
        Loop

'verifica se teve exito no login. Caso negativo, avisa o usuário e encerra a macro
i = IE.Document.body.getElementsbytagname("table")

If i Is Nothing Then GoTo continua

    If InStr(i.innertext, "Usuário ou senha inválido") > 0 Then
        IE.Quit
        MsgBox "Usuário ou senha inválido!", vbCritical, "Login"
        Exit Sub
    End If
    

continues: Set link = IE.Document.body.getelementsbytagname("a") For Each j In link If j.innertext = "APAC" Then j. Click Exit For End If Next j

End Sub

Edited 17/02:

MENU PRINCIPAL:

<div id="reg" style="left:297;width:100" onmouseover="handleMouseOver(event || evt)" onmouseout="handleMouseOut(event)" onclick="scheduleHideTree2(Tree.getElementNode(this))" class="">
    REGULAÇÃO
</div>


NIVEL 1:

<div id="reg_apac" onmouseover="handleMouseOver(event || evt)" onmouseout="handleMouseOut(event)" class="">
    APAC
</div>


NIVEL 2:

<div id="reg_preenchimento_solic" onmouseover="handleMouseOver(event || evt)" onmouseout="handleMouseOut(event)" onclick="setUrl(this,' http://siga.saude.prefeitura.sp.gov.br:80/sms/preenchimentoLaudo.do?method=initUseCaseAndInsert&amp;subsystem=apac')" class=""> 
    Preenchimento do Laudo de Solicitação
</div>  

Menu image: https://imgur.com/a/tX10HSs

HTML REGULACAO

CLIQUE

ERRO SCRIPT

  • if it is vba, remove the tag of javascript

  • It depends a lot on the page’s HTML, @Diego. You don’t need to move your mouse to put it on top of the menu, no. These menus are usually an organized set of tags DIV, SPAN, TABLE and related, which are hidden and have the visibility adjusted as the user points to one or the other. What you can do is look at which HTML element you want to click on, to send an event click to it. Using Chrome, right-click the element, then Inspecionar. If you haven’t opened in the exact place, repeat the operation. Paste the HTML into the question.

  • Opa Cesar... first, thank you for your attention! I put the HTML code of each menu and submenu, see if this is how Oce requested. I took advantage and uploaded the menu image, marking the one that refers to the HTML codes I pasted. Thank you

  • @Césarrodriguez, good morning! I added an image with the HTML of the beginning... the menu I am trying to access is the "REGULATION". Thank you

1 answer

0

Salve, Diego!

The place you will click just runs this script setUrl(this,' http://siga.saude.prefeitura.sp.gov.br:80/sms/preenchimentoLaudo.do?method=initUseCaseAndInsert&amp;subsystem=apac'). This script does nothing but navigate to the page. Therefore, I would simply do the following (...).

EDIT: Since it didn’t work, the second attempt is to run the same javascript that it runs when clicking (adapting, of course, the this of the function for the element that would be referenced by it). The changes were the variable declaration JanelaAtual, at the beginning, and the final block, after the marker continua.

Sub Robo_APAC_1()
    Dim JanelaAtual As HTMLWindowProxy
    Dim IE As InternetExplorer
    
    'Cria um objeto Internet Explorer
    Set IE = New InternetExplorer
        
    'Torna o objeto visível
    IE.Visible = True
    
    'Navega ao site do SIGA Saúde
    IE.navigate "http://siga.saude.prefeitura.sp.gov.br/sms/login.do?method=logoff"
            
    'Identifica se a página já foi totalmente carregada
    While IE.readyState <> READYSTATE_COMPLETE
    Wend
     
    'aguardar 3 segundos (se necessário)
    'sng = Timer
    'Do While sng + 3 > Timer
    'Loop
    
    IE.document.all("j_username").Value = frmLogin.txbLogin
    IE.document.all("j_password").Value = frmLogin.txbSenha
    IE.document.all("confirmar").Click
    
    'fehca a tela te login
    Unload frmLogin
        
        'aguardar 2 segundos antes de validar o logon
        sng = Timer
        Do While sng + 2 > Timer
        Loop
    
    'verifica se teve exito no login. Caso negativo, avisa o usuário e encerra a macro
    i = IE.document.body.getElementsByTagName("table")
    
    If i Is Nothing Then GoTo continua
    
    If InStr(i.innerText, "Usuário ou senha inválido") > 0 Then
        IE.Quit
        MsgBox "Usuário ou senha inválido!", vbCritical, "Login"
        Exit Sub
    End If
        
continua:
    Set JanelaAtual = IE.document.parentWindow
    JanelaAtual.execScript "setUrl(this, 'http://siga.saude.prefeitura.sp.gov.br:80/sms/preenchimentoLaudo.do?method=initUseCaseAndInsert&amp;subsystem=apac');"    
End Sub

Test and tell me if it worked.

  • Cesar, good afternoon to you! I had tried to do this way, but when it tries to navigate to this link, the system terminates the user’s session and when I try to log in again, it falls on the home screen. I’ll put the full code of the location I need to click... once again, thanks for your help.

  • Ok. Second try: run javascript that click would run. Tell if it worked.

  • Save Cesar... when executing the current window.execScript, gave the following error: "Error at runtime '-2147352319 (80020101)': Could not complete the operation. Error: 80020101."Vlw!

  • Maybe it’s the semicolon at the end. Try putting setUrl(document.getElementById('reg_preenchimento_solic'), 'http://siga.saude.prefeitura.sp.gov.br:80/sms/preenchimentoLaudo.do?method=initUseCaseAndInsert&amp;subsystem=apac');" (the only change is the semicolon between the closed parenthesis and the double quotes).

  • Javascript has to finish the semicolon instructions, but the browser console dispenses, so I forgot.

  • Opa master.... is stopping at the same point, with the same error

  • One more try: put the "this", exactly as it is there in the link. See if it worked.

  • It didn’t happen either. What I was thinking: in the line of code you passed, getElementByID is in quotes, which makes it a string and VBA does not understand as a command. I tried to set the object and put it outside the quotation marks, but I am making an error when setting the object (incompatible types). It was like this: Set ThisObj = IE.Document.getElementById("reg_preenchimento_solic")&#xA; JanelaAtual.execScript "setUrl(" & ThisObj & ", ' http://siga.saude.prefeitura.sp.gov.br:80/sms/preenchimentoLaudo.do?method=initUseCaseAndInsert&amp;subsystem=apac');" . Does it make sense? VLW

  • That’s not it, because the function argument execScript is a string (the script to be run). VBA does not even mess with the content of the script. What may be happening is an error in javascript, which returns to the browser as an error and the browser is not displaying. I’ve been through it these days.

  • But I use Selenium to control Chrome, and Selenium returns the browser error.

  • Do the following... Add a stop point to the line JanelaAtual.execScript and ride your code. When it reaches the stop point, you go to Internet Explorer, open the console and play in the browser console the javascript code (setUrl(this, 'http://siga.saude.prefeitura.sp.gov.br:80/sms/preenchimentoLaudo.do?method=initUseCaseAndInsert&amp;subsystem=apac');). Then we will know if it is in javascript the error.

  • In theory, when running the script, it should do the same thing that happens when you click on the menu option.

Show 7 more comments

Browser other questions tagged

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