2

I have a macro that should log me into a website at the push of a button. However, it does not. someone already tried to help me but it was a bit over his head, as he said himself. So here I am trying again with more details!

This is the last code he send me.

Public Sub GetInfo()
    Dim IE As New InternetExplorer
    With IE
        .Visible = True
        .navigate "https://apiweb.biomerieux.com/login"
        While .Busy Or .readyState < 4: DoEvents: Wend
        With .document.getElementById("signupEmail")
            .Focus
            .Value = "xxxx"
        End With
        Application.Wait Now + TimeSerial(0, 0, 1)
        With .document.getElementById("signupPassword")
            .Focus
            .Value = "yyyy"
        End With
        On Error Resume Next
        With .document.getElementById("signupSubmit")
            .Focus
            .Click
        End With
        '.Quit
    End With
End Sub

The weirdest thing is, when you enter the credentials via the macro into the textboxes and click the button, the site thinks the credentials are NOT entered, even when they ARE entered.

My previous helper thought that maybe part of the javascript on the site is somehow messing it up.

QHarr
  • 80,579
  • 10
  • 51
  • 94
Stimpy
  • 25
  • 3

2 Answers2

3

If you inspect the html and its associated events you will see there are a number of event listeners associated with those login fields. In reality, you can get away with just adding the input event and then firing it. Normally, when typing into the input box manually this event would have been caught by the event listener.

Having seen @SIM's answer: the reason mine works is because it generates the event that would otherwise have been picked up by the existing event listeners with the delay as given in their answer. IMO their approach is nicer provided you use a timed loop.


View of one of the event listeners:

enter image description here


VBA:

Option Explicit
Public Sub Login()
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate2 "https://apiweb.biomerieux.com/login"
        While .busy Or .readyState <> 4: DoEvents: Wend

        With .document
            Dim evt As Object
            Set evt = .createEvent("HTMLEvents")
            evt.initEvent "input", True, False

            With .querySelector("#signupEmail")
                .Value = "abc@gmail.com"
                 .dispatchEvent evt
            End With

            With .querySelector("#signupPassword")
                .Value = "defghij"
                .dispatchEvent evt
            End With
            .querySelector("table a").Click
        End With
        Stop '<==delete me later
        .Quit
    End With
End Sub

Reading:

  1. Creating HTML Events
  2. Event Listeners
QHarr
  • 80,579
  • 10
  • 51
  • 94
3

I think this simpler approach is enough to get the job done. All you need to do is let the script wait for the html elements of the first input to be available.

Sub LogMeIn()
    Dim IE As New InternetExplorer, post As Object, HTML As HTMLDocument

    With IE
        .Visible = True
        .navigate "https://apiweb.biomerieux.com/login"
        While .Busy Or .readyState <> 4: DoEvents: Wend
        Set HTML = .document

        With HTML
            Do: Set post = .querySelector("#signupEmail"): DoEvents: Loop While post Is Nothing
            .querySelector("#signupEmail").innerText = "imc@yahoo.com"
            .querySelector("#signupPassword").innerText = "abc12345"
            .querySelector("a > #signupSubmit").Click
        End With
    End With
End Sub
SIM
  • 21,537
  • 4
  • 35
  • 94
  • Yay! It indeed works! I know jack about JS. Hell, I barely understand VBA, so this has been a learning experience. Thanks a lot for the help! – Stimpy Jul 23 '19 at 11:13