Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Website does not recognize my inputs [how to fire IE dom event manually from VBA]

I woud like to buy on gdax automatically. But my inputs in the Amount window doesn´t get recognized. I can see that on the little field, that says: Total (LTC) ≈ 0.00000000

My code:

Sub test()

    Dim ObjIE As New InternetExplorer
    Dim Ohtml As HTMLDocument
    Dim HTMLtags As IHTMLElementCollection
    Dim HTMLtag As IHTMLElement
    Dim HTMLobjekt As IHTMLElement
    Dim item_limit As Object
    Dim y As Integer

    With ObjIE
        .Visible = True
        .navigate "https://www.gdax.com/trade/LTC-EUR"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set Ohtml = .document
    End With

    'Amount
    Do
        Set HTMLtags = Ohtml.getElementsByClassName("OrderForm_input-box_XkGmi")
        DoEvents
    Loop While HTMLtags.Length = 0
    For Each HTMLtag In HTMLtags
        If HTMLtag.Children(1).innerText = "EUR" Then
            Set HTMLobjekt = HTMLtag.Children(0)
            HTMLobjekt.Value = 100      ' this is the part that i excanged
        End If
    Next HTMLtag

    'get the Total(LTC) to cross check
    Do
        Set HTMLtags = Ohtml.getElementsByClassName("OrderForm_total_6EL8d")
        DoEvents
    Loop While HTMLtags.Length = 0
    For Each HTMLtag In HTMLtags
        Debug.Print HTMLtag.innerText & "Total(LTC)"
    Next HTMLtag

End Sub

This is what the website says when the code is done:

Picture.this is how it is

and this is how it should look like, and looks when I type the number in manually:

Picture.this is how it should be

I also exchange the marked part with things like:

HTMLobjekt.innerText = 100

or

HTMLobjekt.innerText = "100"

or

HTMLobjekt.Value = "100"

but nothing worked.

like image 481
Nils Avatar asked Feb 04 '18 18:02

Nils


2 Answers

You need to make sure the page is fully loaded before you take any initiative. I checked for the availability of one such class generated dynamically OrderBookPanel_text_33dbp. Then I did the rest what you tried to do. Lastly, you need to Focus the target before putting that amount in the placeholder. Applying all of the above, your script should more like below:

Sub Get_Value()
    Dim HTML As HTMLDocument, tags As Object
    Dim tag As Object, ival As Object

    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "https://www.gdax.com/trade/LTC-EUR"
        While .Busy = True Or .readyState < 4: DoEvents: Wend
        Set HTML = .document

        Do: Set tags = HTML.querySelector("[class^='OrderBookPanel_text_']"): DoEvents: Loop While tags Is Nothing
        Do: Set tag = HTML.querySelector("input[name='amount']"): DoEvents: Loop While tag Is Nothing
        tag.Focus
        tag.innerText = 100

        Set ival = HTML.querySelector("[class^='OrderForm_total_']")
        [A1] = ival.innerText
        .Quit
    End With
End Sub

Output at this moment:

0.79139546
like image 90
SIM Avatar answered Nov 02 '22 18:11

SIM


Not a complete answer, just a direction. Open the webpage https://www.gdax.com/trade/LTC-EUR in a browser (e. g. Chrome). Click to open context menu on the target element (step 1 on the below screenshot), click Inspect (2), from opened developer tools on the right you can see target element (3), and that there is a bunch of event listeners attached to target object (4). One of the handlers is input on the document node level. Actually the amount is updated by this event handler, when event is bubbled from <input> node. You can easily check that by deleting all other event handlers (click on their Remove buttons). But if you delete particularly this input handler (5) then there will be no update (until you reload the webpage).

F12

So, to mimic user activity you need to create such input event object and dispatch it to the target <input> node. Note <input> node and input control event is completely different things just having the same name.

Tracing event handler execution in IE can be done in debugger. Open IE developer tools (press F12), go to Debugger tab (step 1 on the below screenshot), Breakpoints tab (2), click Add event breakpoint (3), choose input event (4). Now any input event will pause code execution and open debugger window (5).

debugger

If you try to type in the amount textbox on the webpage, debugger will show event handler function code:

handler

You may resume execution (F5), or make step into / over / out. To see event object passed to the function, type arguments in console, there is output for standard event handler call after typing manually:

called by typing

I tested the below test code to trigger that event from VBA:

Sub Test()

    Dim oEvt As Object

    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .Navigate "https://www.gdax.com/trade/LTC-EUR"
        Do While .ReadyState < 4 Or .Busy
            DoEvents
        Loop
        With .Document
            Do While IsNull(.querySelector("div.OrderForm_input-box_XkGmi input"))
                DoEvents
            Loop
            Set oEvt = .createEvent("Event")
            oEvt.initEvent "input", True, False, .parentWindow, 1
            With .querySelector("div.OrderForm_input-box_XkGmi input")
                .Focus
                .Value = "1000"
                .dispatchEvent oEvt
                Stop
            End With
        End With
    End With

End Sub

As a result, absolutely the same actions take place as in case of typing the amount manually. It works without any errors. But Total (LTC) isn't updated. Debugger pauses execution as well, and the output for event object is as follows:

called by VBA

The only difference is that isTrusted property is True for standard call, and False for call via VBA. I guess that is why updating is skipped somewhere in the handler code. I tried to trace the entire code execution till event handler completion, but seems that is huge reverse engineering work, which I can't devote time for at the moment.

Currently I'm stucked in my investigations at that point.

like image 5
omegastripes Avatar answered Nov 02 '22 16:11

omegastripes