href - clicking in a button - no more ideas

FelipeVaz

New Member
Joined
Sep 30, 2014
Messages
48
Hi,

Im having an error when Im trying to execute a code, because I just have class name and href for it. Ive read a lot of methods and none of then worked for me, off course because Im new in VBA. Could you suggest a way to go?

HTML

VBA Code:
<a class="ctl00_Menu_1 CWIMenuStaticMenuItemStyle ctl00_Menu_3" href="/URS/Pages/Cadastros/cadRelatorioTemplate.aspx" style="border-style:none;font-size:1em;margin-left:16px;">Relatórios</a>

VBA

VBA Code:
Public Sub clickbutton()
    Dim ie As New InternetExplorerMedium
   
    With ie
        .Visible = True
        .Navigate2 "MY SITE"

        While .Busy Or .ReadyState <> 4: DoEvents: Wend

        'find the textbox and fill value
        .Document.getElementById("LoginRpt_UserName").Value = "MY USER"
        .Document.getElementById("LoginRpt_Password").Value = "MY PASSWORD"

        'Click the button
        .Document.getElementsByName("LoginRpt$LoginButton")(0).Click
  TRY1      'ie.Document.getElementById("ctl00_Menun2").Click
  TRY2      'ie.Document.querySelector("a[href='/URS/Pages/Cadastros/cadRelatorioTemplate.aspx']").Click
  TRY3      'ie.Document.querySelector("a.ctl00_Menu_1 CWIMenuStaticMenuItemStyle ctl00_Menu_3 CWIMenuStaticSelectedStyle ctl00_Menu_9[href^='/URS/Pages/Cadastros/cadRelatorioTemplate.aspx']").Click
        
    End With
    
End Sub
 

FelipeVaz

New Member
Joined
Sep 30, 2014
Messages
48
No, I mean the Url, not the inspected surce code (I don't want to decode the html to guess how the page is built and which are the elements to spot on).
I don't need your banking account and the password (if you wish you may publish them :)), the Url should be enough

Of course if you specify also what you wish to obtain (I guess you would like to enter login and password, then "Login") that would prevent any misundertanding

Bye

ups, my bad...

URL: http://urs/URS/Pages/login.aspx
 

Some videos you may like

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Anthony47

Well-known Member
Joined
Mar 29, 2006
Messages
1,939
Now it'll be easier...

Try, in a NEW Vba Standard Module:
Code:
 '   >>> STRICTLY ON TOP OF A STANDARD VBA MODULE <<<

#If VBA7 Then
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Sub LoggggIn()
Dim IE As Object, myUrl As String
'
Set IE = CreateObject("InternetExplorer.Application")
myUrl = "http://urs.unimedpoa.com.br/URS/Pages/login.aspx"
With IE
    .Visible = True         '... IE visible
    .Navigate myUrl         '... go
    Sleep 100
    Do While .Busy: DoEvents: Sleep (20): Loop  'Attesa not busy
    Do While .ReadyState <> 4: DoEvents: Sleep (20): Loop 'Attesa documento
End With
Sleep 500
'Form is now ready; set username and password & login
With IE
    .document.getElementById("LoginRpt_UserName").Value = "MyLogin"
    .document.getElementById("LoginRpt_Password").Value = "MyPassword"
    .document.getElementById("LoginRpt_LoginButton").Click
    Sleep 100
    Do While .Busy: DoEvents: Sleep (20): Loop  'Attesa not busy
    Do While .ReadyState <> 4: DoEvents: Sleep (20): Loop 'Attesa documento
End With
'
If IE.LocationURL <> myUrl Then
    'if here, login probably ok
    Debug.Print IE.LocationURL, "Ok?"
    MsgBox ("Login completed?")
Else
    'if here, failed
    Debug.Print IE.LocationURL, "Failed"
    MsgBox ("Login failed" & vbCrLf & _
       Replace(IE.document.getElementById("LoginRpt_pnlLogin").innertext, "Alterar Senha", "", , , vbTextCompare))
End If
'.
'. other commands
'.
'.
End Sub

Bye
 

FelipeVaz

New Member
Joined
Sep 30, 2014
Messages
48

ADVERTISEMENT

Now it'll be easier...

Try, in a NEW Vba Standard Module:
Code:
 '   >>> STRICTLY ON TOP OF A STANDARD VBA MODULE <<<

#If VBA7 Then
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Sub LoggggIn()
Dim IE As Object, myUrl As String
'
Set IE = CreateObject("InternetExplorer.Application")
myUrl = "http://urs.unimedpoa.com.br/URS/Pages/login.aspx"
With IE
    .Visible = True         '... IE visible
    .Navigate myUrl         '... go
    Sleep 100
    Do While .Busy: DoEvents: Sleep (20): Loop  'Attesa not busy
    Do While .ReadyState <> 4: DoEvents: Sleep (20): Loop 'Attesa documento
End With
Sleep 500
'Form is now ready; set username and password & login
With IE
    .document.getElementById("LoginRpt_UserName").Value = "MyLogin"
    .document.getElementById("LoginRpt_Password").Value = "MyPassword"
    .document.getElementById("LoginRpt_LoginButton").Click
    Sleep 100
    Do While .Busy: DoEvents: Sleep (20): Loop  'Attesa not busy
    Do While .ReadyState <> 4: DoEvents: Sleep (20): Loop 'Attesa documento
End With
'
If IE.LocationURL <> myUrl Then
    'if here, login probably ok
    Debug.Print IE.LocationURL, "Ok?"
    MsgBox ("Login completed?")
Else
    'if here, failed
    Debug.Print IE.LocationURL, "Failed"
    MsgBox ("Login failed" & vbCrLf & _
       Replace(IE.document.getElementById("LoginRpt_pnlLogin").innertext, "Alterar Senha", "", , , vbTextCompare))
End If
'.
'. other commands
'.
'.
End Sub

Bye

Thank you. Error message; "the remote server machine does not exist or is not available"

1584559431871.png
 
Last edited:

FelipeVaz

New Member
Joined
Sep 30, 2014
Messages
48

Ive adjust the code and it works, changed:

THIS: Set IE = New InternetExplorerMedium

INSTEAD OF: Set IE = CreateObject("InternetExplorer.Application")

Let me understand whats going on, this code works for logging in the website, ok?

Beyond this, I need to click in the button that has the href = "/URS/Pages/Cadastros/cadRelatorioTemplate.aspx"

HTML from website, that piece Ive put before...

VBA Code:
<a class="ctl00_Menu_1 CWIMenuStaticMenuItemStyle ctl00_Menu_3" href="/URS/Pages/Cadastros/cadRelatorioTemplate.aspx" style="border-style:none;font-size:1em;margin-left:16px;">Relatórios</a>
 

Anthony47

Well-known Member
Joined
Mar 29, 2006
Messages
1,939
These automatizations are a sort of trial and error, and different Pc with different configurations can give different results…

And, by the way, you today are the second case where the problem is fixed using InternetExplorerMedium instead of InternetExplorer (see Trying to open webpage and save it as a pdf).

The article you linked doesn't seem to refer to our case, because every property or method is correctly referred to its parent object; and indeed the solution seem to be using InternetExplorerMedium instead of InternetExplorer [the object that is created by CreateObject("InternetExplorer.Application")]

I understand that your need is "clicking" a button that is beyond the login phase, an area that I am not able to enter as I lack the autorization.

You should inspect the source html code, maybe you can find reliable hooks there.
I can only replay the same snippet I suggested in an earlier message, linked the following way to your login process:
Code:
    .Document.getElementsByName("LoginRpt$LoginButton")(0).Click          'Login Button
    Sleep 100
    Do While .Busy: DoEvents: Sleep (20): Loop  'Attesa not busy
    Do While .ReadyState <> 4: DoEvents: Sleep (20): Loop 'Attesa document
End With
Set aColl = IE.document.getElementsByClassName("ctl00_Menu_1 CWIMenuStaticMenuItemStyle ctl00_Menu_3")
For Each myItm In aColl
    If UCase(Right(myItm.href, 26)) = "/cadRelatorioTemplate.aspx" Then
        myItm.Click
        Exit For
    End If
Next myItm

Note that it uses Sleep, therefore on top of the vba module you need to insert the declaration of the library "kernel32" (the lines #If VBA7 Then /#Else / #End If)

Bye
 

Watch MrExcel Video

Forum statistics

Threads
1,114,419
Messages
5,547,819
Members
410,813
Latest member
Vhinzvirgo
Top