Click text input box based on id

Viewed 77

I am currently trying to click the following text input box based on its id: dp1571754895218 in VBA

<input title="INVOICEDATE" class="half-filter hasDatepicker" id="dp1571754895218" style="margin-right: 2px; opacity: 1;" type="text" placeholder="From" data-bind="attr: { placeholder: 'From', title: $data.Key, 'data-tabgroup': 'itemlistfilter', 'data-tabgroupindex': ($index() + 1) }, datepicker: $data.Range.From, datepickerOptions: { dateFormat: $root.jQformatDate($data.Format) }, style: { 'opacity': $parent.multiSearchOn() ? '0.3' : '1' }" data-tabgroup="itemlistfilter" data-tabgroupindex="7">

What I have tried so far is this:

Dim date1 As Object
Set html = ie.document
Set date1 = html.getElementById("dp1571754895218")
For Each l In date1
    If l.className = "half-filter hasDatepicker" Then
        l.Click
        Exit For
    End If
Next

But I keep getting the following error: Run-time error '424': Object required. I already have the correct references too. What am I doing incorrectly?

Edit: For QHarr, here are both html codes placeholder = To:

<input title="INVOICEDATE" class="half-filter hasDatepicker" id="dp1571841444746" style="opacity: 1;" type="text" placeholder="To" data-bind="attr: { placeholder: 'To', title: $data.Key, 'data-tabgroup': 'itemlistfilter', 'data-tabgroupindex': ($index() + 1.1) }, datepicker: $data.Range.To, datepickerOptions: { dateFormat: $root.jQformatDate($data.Format) }, style: { 'opacity': $parent.multiSearchOn() ? '0.3' : '1' }" data-tabgroup="itemlistfilter" data-tabgroupindex="7.1">

Placeholder = From:

<input title="INVOICEDATE" class="half-filter hasDatepicker" id="dp1571841444745" style="margin-right: 2px; opacity: 1;" type="text" placeholder="From" data-bind="attr: { placeholder: 'From', title: $data.Key, 'data-tabgroup': 'itemlistfilter', 'data-tabgroupindex': ($index() + 1) }, datepicker: $data.Range.From, datepickerOptions: { dateFormat: $root.jQformatDate($data.Format) }, style: { 'opacity': $parent.multiSearchOn() ? '0.3' : '1' }" data-tabgroup="itemlistfilter" data-tabgroupindex="7">

1 Answers

Some possibilities (the first being the likely culprit then some other considerations are listed):

1) getElementById returns a single element so you do not For Each over. Simply do:

html.getElementById("dp1571754895218").click

2) The id may be dynamic. Then try using combination of title and class

ie.document.querySelector("[title=INVOICEDATE].hasDatepicker").click

No need for a loop

3) You do not have a long enough wait for the element to be present. Use a proper page load wait and a timed loop for the presence of the element

Public Sub test()
    Dim ie As New InternetExplorer, t As Date, ele As Object
    Const MAX_WAIT_SEC As Long = 10


    With ie
        .Visible = True
        .Navigate2 "url"

        While .Busy Or .readyState < 4: DoEvents: Wend
        t = Timer
        Do
            On Error Resume Next
            Set ele = .document.getElementById("dp1571754895218")
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While ele Is Nothing

        If ele Is Nothing Then Exit Sub

        ele.click

        Stop
        .Quit
    End With
End Sub

4) Your element is inside a frame/iframe which needs to be navigated first


EDIT:

Try:

ie.document.querySelector(".hasDatepicker[placeholder=To]").value = "ABC"
ie.document.querySelector(".hasDatepicker[placeholder=From]").value = "XYZ"
Related