1

Trying to simulate onfocus and typing event, but it not work

Sub Login(MyLogin, MyPass)
    Dim IEapp As InternetExplorer
    Dim IeDoc As Object
    Dim ieTable As Object
    TaskKill "iexplore.exe"

    Set IEapp = New InternetExplorer
        IEapp.Visible = True
            IEapp.Navigate "https://example.com/portal/en/login"
        Do While IEapp.Busy: DoEvents: Loop: Do Until IEapp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
        Set IeDoc = IEapp.Document
        With IeDoc.forms(2)


            .Name.Value = MyLogin
            .Name.Focus
            .FireEvent ("onkeypress")
            .FireEvent ("onchange")


            .Password.Value = MyPass
            .Password.Focus
            .FireEvent ("onkeypress")
            .FireEvent ("onchange")
       End With
        IeDoc.getElementsByClassName("form__button form__button--login-site")(1).Click

End Sub

How to call focus and typing events? Sendkeys is bad solution as it have Excel bug with Numlock

Dmitrij Holkin
  • 1,995
  • 3
  • 39
  • 86

2 Answers2

3

The event listeners for those elements indicate input events are watched for. You can create those and then fire.

Internet Explorer:

Option Explicit
Public Sub LogIn()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "https://www.darsgo.si/portal/en/login"

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

        .document.querySelector(".LoginHeader + p a").Click

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

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

        With .document.querySelector("#name")
            .Value = "bobBuilder@banana.com"
            .dispatchEvent event_onInput
        End With
        With .document.querySelector("#password")
            .Value = "something"
            .dispatchEvent event_onInput
        End With

        .document.querySelector(".form__button").Click

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

        Stop
        .Quit
    End With     
End Sub

Selenium:

If you are prepared to use selenium basic it works just fine as follows. After installing selenium go VBE > Tools > References and add a reference to selenium type library. You should use the latest ChromeDriver. The ChromeDriver may come installed already in the selenium folder - otherwise it needs to be added there.

Option Explicit

Public Sub Login()
    Dim d As WebDriver
    Set d = New ChromeDriver
    Const URL = "https://www.darsgo.si/portal/en/login"
    With d
        .Start "Chrome"
        .get URL
        .FindElementByCss(".choose-language-popup__list li:nth-of-type(2) a").Click
        .FindElementByCss(".choose-language-popup__icon-continue").Click
        .FindElementByCss("p.registerHeader a").Click
        .FindElementById("name").SendKeys "bob@builder.com"
        .FindElementById("password").SendKeys "verySecret"
        .FindElementByCss(".form__button").Click

        Stop

        .Quit
    End With
End Sub
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Just for curiosity sake, would you care to explain more in depth what the code in your first snippet does? quarySelector gets the first element that matches the specifications, but where did you get `".LoginHeader + p a"` from? Is that readable in the website's source? – JvdV Feb 19 '19 at 16:49
  • 1
    The page has event listeners watching those elements for input events. The code simply adds those events to the page and triggers them after the values have been inputed. This was what was being triggered manually by adding text to the field. – QHarr Feb 19 '19 at 16:53
  • Nice, I get the theory behind it now. Putting it into practice will require some more research (and trial and error) ... :) – JvdV Feb 19 '19 at 16:57
  • See https://codedocu.com/Office-365/Access/Controls/web-Browser/Vba,-HTMLSelect_colon_-FireEvent-OnChange-or-DispatchEvent?1890 When you right click on the input elements and observe in the dev tools elements window you should be able to see the listeners listed. – QHarr Feb 19 '19 at 16:58
  • Thanks, I see where to get the elements from, still raises a question where `".LoginHeader + p a"` comes from. – JvdV Feb 19 '19 at 17:07
  • when I initially access the start url I have to click a link for already registered login as opposed to create an account. If you step through with F8 hopefully you should see this. – QHarr Feb 19 '19 at 17:17
  • I am stepping through, come by that same screen, but I just can't seem to find that name in the dev tools elements window. Either way, there is a small error in your code with `.Quit`. – JvdV Feb 19 '19 at 17:24
  • It is a css selector. Say you open in google chrome > elements tab > select any html element > press Ctrl + F > enter the selector in the search box > hit enter. You have to be at the right url though. I will paste a link in a moment. – QHarr Feb 19 '19 at 17:28
  • 1
    See the following: note the highlighted element and the selector in the top right search box https://filebin.net/wtq37bhygmmb90pl – QHarr Feb 19 '19 at 17:32
  • 1
    note from the bottom of the window you can see the element path. This indicates there is actually a better css selector of simply: p.registerHeader a – QHarr Feb 19 '19 at 17:35
  • 1
    ahhhhh now I see. I was confused cause the other elements name and password were so clear to me. I have zero css/java/HTML background, learned VBA just practicing, but these type of things still amazes me! Thank you for the explaination. I hope to be able to use it in the future! P.S. dont forget to edit your `.Quit` ;) – JvdV Feb 19 '19 at 17:41
  • 1
    thank you. See the following: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors and play with this https://flukeout.github.io/ – QHarr Feb 19 '19 at 18:00
  • Maybe you can solve problem with wicked-crypt site? How to get dynamics links with dynamics elements – Dmitrij Holkin Feb 20 '19 at 09:31
  • Please post a new question and leave a link and I will have a look. – QHarr Feb 20 '19 at 09:50
1

I think this would work for you:

Sub Login()
    Dim IEapp As InternetExplorer
    Dim IeDoc as Object
    Dim ieTable As Object

    TaskKill "iexplore.exe"
    Set IEapp = New InternetExplorer
        IEapp.Visible = True
            IEapp.navigate "https://example.com/portal/en/login"
        Do While IEapp.Busy: DoEvents: Loop: Do Until IEapp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
        Set IeDoc = IEapp.document
        With IeDoc.forms(2)
            .elements("name").Value = MyLogin
            .elements("password").Value = MyPass
       End With
       IeDoc.forms(2).submit
End Sub
Dmitrij Holkin
  • 1,995
  • 3
  • 39
  • 86
JvdV
  • 70,606
  • 8
  • 39
  • 70
  • result the same, as page want to user focus and type into field – Dmitrij Holkin Feb 19 '19 at 12:58
  • form submits, but not going to login as there are no focused and typed fields controled by javascript, I think submit goes after click but not by submit as it will be prevent.default – Dmitrij Holkin Feb 19 '19 at 14:04
  • @DmitrijHolkin, that's unfortunate. Was worth a try. I'm very new to the VBA-internet connection and am very curious to know if solution provided by Qharr worked for you. – JvdV Feb 19 '19 at 16:44