如何使用querySelector通过<a title=""> in VBA

时间:2019-05-06 19:57:18

标签: vba

I am trying to use a querySelector().Click to click a button in a browser.

Here is the HTML Code:

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

Here is what I tried:

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

This is the error message I get:

Run-time error '424': Object required

The debug highlights this portion of the code

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

Yes, I did assign an object name and its preceding codes work perfectly fine. But if I click the "debug" button and run the code again, it works strangely.

1 个答案:

答案 0 :(得分:0)

尝试按标签索引捕获对象,在标签<a>的大小写索引上,您可以在html元素之间循环,以根据案例title="QUERYSELECTOR HERE"的属性获得所需的元素 ;该代码在<a>内部的("td")(0)标签之间循环,并搜索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

希望这会有所帮助。