VBA loggin into a website

Ograda

New Member
Joined
Mar 7, 2014
Messages
7
Hello guys, first i'm in need of a code that logs in into an website to collect it's information.
I've been trying it in a hole different ways, but none seems to work, i've tryed many codes and kinds, i've seen some problems that i'd not been able to solve.

First of all:
The website never gets ready (readystate 4 never occurs)

Second problem:
if i put a loop waiting for busy ends or readystate 4, it throws me out of the execution.

Third problem:
I got some problems refeering the cells that i need to fill in HTML document.

well, here goes the HTML code:
HTML:
<body ******="form2.elements[0].focus()" >    
<div id="box_login">        
<div class="td1Titulo">Identificação</div>        
<div class="td1fundoclaro">            
<div id="mensagem_erro"></div><br />            
<form name="form2" id="form_login" method="post" action="testa_login.asp">                
<div class="login_info">                    Preencha os campos para acessar a área restrita                 </div><br />                
<div class="login_info left">                    
<div class="label_50"><label for="txtUsuario">Login: </label></div>                    
<input type="text" name="txtUsuario" size="10" class="botao required">                </div>                
<div class="login_info left">                    
<div class="label_50"><label for="password">Senha: </label></div>                    
<input type="password" name="txtSenha" size="10" class="botao required">                </div>                
<div class="login_info">                    
<input type="submit" name="Entrar" value=" Entrar " class="botao">                    
<input type="reset" name="Limpar" value="Limpar" class="botao">                
</div>                
<div class="login_info">                    <br /> Não sabe ou esqueceu sua senha? <br />                    
<a href="http://intranet.ad.finep.gov.br/gerenciador/Senha/senha.asp">Clique aqui</a>                     para gerar uma nova e recebê-la por correio eletrônico. <br /><br />                    Para alterar sua senha da intranet:                     
<a href="http://intranet.ad.finep.gov.br/gerenciador/Senha/troca_senha.asp">Alterar                   senha</a><br /><br />                   
<a href="javascript:history.back()" class="rodape">Voltar</a>                
</div>              
</form>        
</div>    
</div>
</body>

i need to fill this forms:
HTML:
then click this button:
[HTML]<input type="submit" name="Entrar" value=" Entrar " class="botao">

i'm using this code at moment, it loads the website, and then stops, i couldn't get further on any other VBA code.

Code:
Sub getValues()Dim objIe As Object, xobj As HTMLDivElement


    Set objIe = CreateObject("InternetExplorer.Application")
    objIe.Visible = True


    objIe.Navigate "http://intranet." 'website


   ' While (objIe.Busy Or objIe.ReadyState <> 4): DoEvents: Wend  ' if i cut of this comment and executs the code until here, it gets me the problem 1


    Set xobj = objIe.Document.getElementById("box_login")
' executing the code without checking for its readystate, returns me error on the line above (problem 2)
    'Set xobj = xobj.getElementsByClassName("login_info").Item(0)
    'Set xobj = xobj.getElementsByClassName("data")(0)


    Debug.Print xobj.innerText


    Set xobj = Nothing


    objIe.Quit
    Set objIe = Nothing
End Sub

the problem 1 is:
"automation error"
"not specifyed error" (translating myself, my Excell is on brazilian portuguese)

the problem 2 is:
"automation error"
"not specifyed error" (translating myself, my Excell is on brazilian portuguese)
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I'd tryed to get into the HTML point that needs to be filled with this code:

Code:
Dim oHtml, myData

Sub getValues()
    ' URL = "http://intranet*"
Dim objIe As Object, xobj As HTMLDivElement


    Set objIe = New InternetExplorer
    objIe.Visible = True


    objIe.Navigate "http://intranet*


    While (objIe.Busy Or objIe.ReadyState <> 4): DoEvents: Wend


    Set xobj = objIe.Document.getElementById("box_login")
    'Set xobj = xobj.getElementsByClassName("login_info")
    'Set xobj = xobj.getElementsByClassName("data")(0)


    Debug.Print xobj.innerText


    Set xobj = Nothing


    objIe.Quit
    Set objIe = Nothing
End Sub

the debug gave me the following:
Identificação




Preencha os campos para acessar a área restrita




Login:
Senha:



Não sabe ou esqueceu sua senha?
Clique aqui para gerar uma nova e recebê-la por correio eletrônico.


Para alterar sua senha da intranet: Alterar senha


Voltar

the exact information that i was looking for at the box, but when i tryed to go further and achieve this line on the code (for login)
HTML:
Login:

uncommenting this line:
Code:
Set xobj = xobj.getElementsByClassName("login_info")

i got the following error:
"the object doesnt accepts this propriety or method"

anyone know how can i get to it?
 
Upvote 0
I got a further advance using this code:
Code:
Set xobj = objIe.Document.getElementById("form_login")    
    Set Elements = xobj.getElementsByTagName("input")
    For Each inputs In Elements
        Debug.Print inputs.innerText
        Debug.Print "found"
Next inputs
HTML:
        'login <input type="text" name="txtUsuario" size="10" class="botao required">        'password <input type="password" name="txtSenha" size="10" class="botao required">        'submit <input type="submit" name="Entrar" value=" Entrar " class="botao">        'clear <input type="reset" name="Limpar" value="Limpar" class="botao">
the inputs are found and "found" is printed 4 times, they all print up blank spaces as they should, but how could i select an input by its name on tag?

for example, the first one "login" how could i select it refeering to it as "txtUsuario", his defined name?

and then, is there a function like "setInnerText" or could i use inputs.innerText = "user" to set it?
 
Last edited:
Upvote 0
problem solved.

Code:
Sub getValues()Dim objIe As Object, inputs As HTMLDivElement
Dim Elements As IHTMLElementCollection
Dim str As String
    Set objIe = New InternetExplorer
    objIe.Visible = True
    objIe.Navigate "http://intranet.*"
    
    While (objIe.Busy Or objIe.ReadyState <> 4): DoEvents: Wend


    Set Elements = objIe.Document.getElementById("form_login").getElementsByTagName("input")
    For Each inputs In Elements
        str = inputs.getAttribute("name")
        If str = "txtUsuario" Then
            inputs.innerText = user
        End If
        If str = "txtSenha" Then
            inputs.innerText = senha
        End If
        If str = "Entrar" Then
            inputs.Click
            Exit For
        End If
    Next inputs




    'objIe.Quit
    'Set objIe = Nothing
End Sub
 
Upvote 0
bad news, i've solved it, tested and it worked yesterday, but when i'd logged in today and tryed to execute the same function without changing a line it wouldn't work
the first stop was on the line:
Code:
[COLOR=#333333] While (objIe.Busy Or objIe.ReadyState <> 4): DoEvents: Wend[/COLOR]
it threw me:
Automation error
Error not specified

i tryed to run commenting this part of code and then the code sttoped on this line:
Code:
Set Elements = objIe.Document.getElementById("form_login").getElementsByTagName("input")
it threw me:
The document method of "IWebBrowser2" has failed

i tryed to set a little delay to load up the website with function sleep
Code:
Sleep 5 * 1000
the i got error on the same line as before
Code:
Set Elements = objIe.Document.getElementById("form_login").getElementsByTagName("input")
but a different one this time:
Automation Error
Unknown Interface

Does anyone has already had or seem a problem like this?
i dont know what is wrong to handle, the code was working yesterday, i don't know any other path to walk through.
 
Upvote 0
I can't reproduce the errors you're getting. The code you posted is working fine. I'd say just make sure your project references 'Microsoft Internet Controls' and 'Microsoft HTML Object Library' still. Also, those are the kinds of weird errors that I think I've fixed by restarting Windows before.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top