【问题标题】:How to use querySelector to click a button by <a title=""> in VBA如何在 VBA 中使用 querySelector 通过 <a title=""> 单击按钮
【发布时间】:2019-05-07 06:54:14
【问题描述】:

我正在尝试使用 querySelector()。单击以单击浏览器中的按钮。

这是 HTML 代码:

<td tabindex="0" title="Company" class="Smell" role="grid" data-table-header="some header">
    <div class="classroom">
        <div class="first inner classroom">
            <div class="second inner classroom">
                <div class="third inner classroom">

                    <a title="QUERYSELECTOR HERE" class="btn-link" href="/Tosomewhere/here/andthere/111">I'm not trying to grab an inner value because other tables have same inner value</a>

                </div>
                <div class="different sub-classroom">
                    <span class="some span">Mrs. Crobapole</span>
                </div>
            </div>
        </div>
    </div>
</td>

这是我尝试过的:

ie.Document.getElementById("Input Simpson").Value = ActiveCell.Value
ie.Document.getElementById("Some button for Simpson").Click
    Do
    DoEvents
    Loop Until ie.ReadyState = 4

ie.Document.querySelector("[title='QUERYSELECTOR HERE']").Click
    Do
    DoEvents
    Loop Until ie.ReadyState = 4

这是我收到的错误消息:

Run-time error '424': Object required

调试突出显示这部分代码

ie.Document.querySelector("[title='QUERYSELECTOR HERE']").Click

是的,我确实指定了一个对象名称,并且它前面的代码工作得很好。但是如果我单击“调试”按钮并再次运行代码,它会奇怪地工作。

【问题讨论】:

  • 看起来不错,您可能等待页面加载的时间不够长。尝试将and ie.busy = false 添加到Until
  • 我添加了and ie.busy = false。但我仍然收到 424 错误。
  • 顺便调试一下ie.Document.querySelector("[title='QUERYSELECTOR HERE']").Click
  • 现在我看,我也认为它与页面加载有关,因为在“加载”javascript图标消失之前显示错误消息
  • this定时循环或these

标签: vba


【解决方案1】:

尝试通过标签索引捕获对象,在标签&lt;a&gt;的案例索引上,您可以在html元素之间循环以根据您的案例title="QUERYSELECTOR HERE"的属性获取所需的对象 ;此代码在("td")(0) 内的&lt;a&gt; 标记之间循环并搜索title 属性。

Dim MyHtmlTable As Object

MyHtmlTable = ie.document.getElementsByTagName("td")(0)
For i = 0 To MyHtmlTable.All.tags("a").Length - 1
If MyHtmlTable.getElementsByTagName("a")(i).getAttribute("Title") = "QUERYSELECTOR HERE" Then
    MyHtmlTable.getElementsByTagName("a")(i).Click
    Do
    DoEvents
    Loop Until ie.readyState = 4
Exit For
End If
Next i

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多