How to automate the login in a web


I have a problem that I have not yet found the solution, I recently registered on a website and I set out to create an automatic login with excel macros. Everything seems to work, but I can not fill in the requested fields and therefore does not log in.

this is the code that I used:

Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer

 Sub auto_open()

 Dim MyHTML_Element As IHTMLElement
 Dim MyURL As String
 On Error GoTo Err_Clear
 MyURL = ""
 Set MyBrowser = New InternetExplorer
 MyBrowser.Silent = True
 MyBrowser.navigate MyURL
 MyBrowser.Visible = True
 Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
 Set HTMLDoc = MyBrowser.document
 HTMLDoc.all.Email.Value = "mi email" 'Aqui pongo mi email de login
 HTMLDoc.all.passwd.Value = "mi contraseña" 'Aqui pongo mi contraseña
 For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
 If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
 If Err <> 0 Then
 Resume Next
 End If
 End Sub

Thanks for your help!

asked by Jensyr 23.12.2018 в 15:06

1 answer


use a css attribute selector

Option Explicit
Public Sub detalles()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True

        .Navigate2 ""

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            .querySelector("[type=email]").Value = "[email protected]"
            .querySelector("[type=password]").Value = "contrasena"
            .querySelector(".form-group [type=submit]").Click
        End With

        While .Busy Or .readyState < 4: DoEvents: Wend
    End With
End Sub
answered by 23.12.2018 / 20:04