DDV
DDV

Reputation: 2384

Failing to Set Input Box Values on Website

I have the following VBA code:

Sub OpenWebPage()

    Dim oIE As Object
    Dim sURL As String
    Dim HTML As HTMLDocument, hDataManager As IHTMLElementCollection, hDropSelect
    Dim HWNDSrc As Long
    Dim itm As Variant


    'objects
    Set oIE = CreateObject("InternetExplorer.Application")


'//---Open the browser and log in, then navigate to the data manager
    oIE.silent = True 'No pop-ups
    oIE.Visible = True

    sURL = "https://publicisuk.lumina.mediaocean.com/Admin/DataManager.aspx"
    oIE.navigate sURL

    'wait for process to complete before executing the next task
    Do While oIE.Busy: DoEvents: Loop
    Do Until oIE.ReadyState = 4: DoEvents: Loop

    'get window ID for IE so we can set it as active window
    HWNDSrc = oIE.HWND
    'set IE as active window
    SetForegroundWindow HWNDSrc

    'loop through elements/ items to find username field
    For Each itm In oIE.document.All
        If itm = "[object HTMLInputElement]" Then
            If itm.Name = "username" Then
                Debug.Print "Username field found!"
                itm.Value = "[email protected]"
                Debug.Print "Username: " & itm.Value
                Exit For
            End If
        End If
    Next itm

    'now loop through to find password element
    For Each itm In oIE.document.All
        If itm = "[object HTMLInputElement]" Then
            If itm.Name = "password" Then
                Debug.Print "Password field found!"
                itm.Value = "IAmAMonkey"
                Debug.Print "Password: " & itm.Value
                Application.SendKeys "~", True
                Exit For
            End If
        End If
    Next itm

What it does is loads Internet Explorer to an object, and then navigates to a URL where I want to log in. I loop through the elements on the web-page checking for InputElements. When the Username input box is found it sets the .Value to the username, and when the Password input box is found it sets the .Value to the password, after which I send the Enter key, presumably to log in.

Now, the interesting problem. When I run this it prints out that it finds the respective fields, and also prints out their newly set values, however, no text appears in the boxes at all.

I have also tried the following syntax:

oIE.Document.getElementById("username").Value = "[email protected]"

oIE.Document.getElementsByName("username").Value = "[email protected]"

With the same result - there is no error, but the values aren't showing up in the boxes.

Does anyone know why this would be the case or what is wrong with my approach?

Below is the HTML code.

<div id="login-panel">

            <input name="username" class="form-required-field" id="username" accesskey="u" type="text" placeholder="Username" value="" data-bind="value: form().username, placeholder: bundle['user.label.html']" htmlescape="true" autocomplete="off" cssclass="form-required-field"><!-- organization select goes here if multiple organizations use the same domain --><div class="inline-organization-panel" id="inline-organization-panel" style="display: none;" data-bind="moSlideVisible: isOrganisationSelectPanelVisible()">
                <span id="inline-organization-label" data-bind="text: bundle['organization.panel.label']">Choose organization.</span>
                <div data-bind="foreach: organisationInfoArray"></div>
            </div>

            <input name="password" class="form-required-field" id="password" accesskey="p" type="password" placeholder="Password" value="" data-bind="value: form().password, placeholder: bundle['password.label.html']" htmlescape="true" autocomplete="off" cssclass="form-required-field" csserrorclass="error"><!-- Continent selection goes here if mf-style username and don't have continent cookie -->

Upvotes: 0

Views: 214

Answers (3)

QHarr
QHarr

Reputation: 84465

I would focus on each element before assigning value then you simply need to remove the disabled attribute from the submit button

Option Explicit

Public Sub LogIn()
    Dim ie As SHDocVw.InternetExplorer

    Set ie = New SHDocVw.InternetExplorer

    With ie
        .Visible = True
        .Navigate2 "https://publicisuk.lumina.mediaocean.com/mo-cas/login"

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

        With .document

            With .querySelector("#username")
                .Focus
                .Value = "[email protected]"
            End With
            With .querySelector("#password")
                .Focus
                .Value = "IAMaMonkey"
            End With
            With .querySelector("#buttonSignin")
                .removeAttribute "disabled"
                .Click
            End With
        End With

        Stop
    End With
End Sub

Upvotes: 1

Yu Zhou
Yu Zhou

Reputation: 12961

I tested and found that the code would run when setting breakpoints before setting value but wouldn't run without breakpoints.

I think it could be that debug mode adds more delay so I tried to add a separate delay in the code and it works. You could check my sample below:

Sub LOADIE()
    Set ieA = CreateObject("InternetExplorer.Application")
    ieA.Visible = True
    ieA.navigate "https://publicisuk.lumina.mediaocean.com/mo-cas/login"
    Do Until ieA.readyState = 4
       DoEvents
    Loop

    delay 4

    Set doc = ieA.Document
    Set UserName = doc.getElementById("username")
    UserName.Value = "[email protected]"
    Set Password = doc.getElementById("password")
    Password.Value = "IAmAMonkey"
    Set btn = doc.getElementById("buttonSignin")
    btn.Disabled = False
    btn.Click
End Sub

Private Sub delay(seconds As Long)
    Dim endTime As Date
    endTime = DateAdd("s", seconds, Now())
    Do While Now() < endTime
        DoEvents
    Loop
End Sub

Upvotes: 0

Nathan_Sav
Nathan_Sav

Reputation: 8531

Here is where I got to, i'll try again a little later, but may help in the meantime.

Dim ie As InternetExplorer
Dim d As HTMLDocument
Dim f As HTMLFormElement

Set ie = New InternetExplorer
ie.Visible = 1
ie.navigate "https://publicisuk.lumina.mediaocean.com/Admin/DataManager.aspx"

Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
    DoEvents
Loop

Set d = ie.document
Set f = d.forms(0)

'Putting a break point here and waiting a moment allows access

f.elements("username").Value = "username"

Upvotes: 1

Related Questions