单击基于ID的文本输入框

时间:2019-10-22 17:29:17

标签: javascript html vba web-scraping

我目前正在尝试根据其ID单击以下文本输入框:VBA中的dp1571754895218

<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">

到目前为止,我尝试过的是:

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

但是我一直收到以下错误:运行时错误'424':需要对象。 我也已经有了正确的参考。我在做什么错?

编辑:对于QHarr,这两个都是html代码 占位符=收件人:

<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">

占位符=来自:

<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 个答案:

答案 0 :(得分:0)

一些可能性(首先是可能的罪魁祸首,然后列出其他一些注意事项):

1)getElementById返回单个元素,因此您不会For Each结束。只需:

html.getElementById("dp1571754895218").click

2)ID可能是动态的。然后尝试结合使用titleclass

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

无需循环

3)您没有足够长的时间等待元素出现。为元素的存在使用适当的页面加载等待和定时循环

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)您的元素位于frame/iframe内部,需要先进行导航


编辑:

尝试:

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