2

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">
Su314
  • 49
  • 7

1 Answers1

0

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"
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • It says that the object doesn't support this property or method on the "ele.click" line – Su314 Oct 23 '19 at 14:09
  • What is the result of debug.print typename(ele) ? – QHarr Oct 23 '19 at 14:10
  • Ah so I took a look at your second line of code and you were right about the dynamic id. The problem with that though is that there are 2 datepickers, one that is $data.Range.From and the other is $data.Range.To. Do you know how I can distinguish the two? Because the 3rd solution you provided is a little difficult to incorporate into my macro for me. – Su314 Oct 23 '19 at 14:34
  • Can you share the html for both? Use the snippet tool via [edit] to insert html properly. – QHarr Oct 23 '19 at 14:35
  • I put the two snippets in the question post :D. What I want to do is type in values into those areas. I thought clicking and then using Sendkeys (vba) to input values from Excel, but if there is a way to directly input these values without clicking, that would be great too. – Su314 Oct 23 '19 at 14:47
  • getAttribute takes a value. It does not assign. So, are you saying you used my bottom edit to assign a value then read it to the sheet and got "Tue Jan 30 2018 00:00:00 GMT-0500 (Eastern Standard Time)" ? – QHarr Oct 23 '19 at 17:18
  • I just tried it now and still got that weird result – Su314 Oct 23 '19 at 17:21
  • I can't see your screen so I don't know what you are doing :-( you need to tell me the steps you are doing to produce this. – QHarr Oct 23 '19 at 17:22
  • What I am trying to do is input the From date and To date into the above html fields. I will be getting these dates from an excel doc. The dates have the format MM/DD/YYYY. Say the From date is in cells(1, 1). When I do ie.document.querySelector(".hasDatepicker[placeholder=To]").value = Cells(1,1).value, it autmatically inputs the date in that weird format. But if I do ie.document.querySelector(".hasDatepicker[placeholder=To]").value = "1/30/2018", it inputs it correctly. :( – Su314 Oct 23 '19 at 17:33
  • try ie.document.querySelector(".hasDatepicker[placeholder=To]").value = Cells(1,1).value2 – QHarr Oct 23 '19 at 20:47
  • Or input as text e.g. ie.document.querySelector(".hasDatepicker[placeholder=To]").value = Format$(Cells(1,1).value, "mm/dd/yyyy") – QHarr Oct 23 '19 at 20:48