如何在VBA(没有标签名称/ ID)的网页上单击为搜索框提供的按钮?

时间:2012-10-01 07:22:44

标签: vba excel-vba excel

我在网页下面有以下代码

<form class="search" target="_blank" method="get" action="Search.mvc/advanced">
    <input aria-haspopup="true" aria-autocomplete="list" role="textbox" autocomplete="off" class="quick ui-autocomplete-input" name="q" id="search-box" placeholder="Search" type="search">
    <button type="button" class="advanced" title="Advanced Search"><span class="icon"></span></button>
</form>

我想点击为搜索框提供的按钮,网页代码没有标签名称或ID。我正在使用IE 8浏览器。

任何人都可以帮助我......

2 个答案:

答案 0 :(得分:1)

尝试使用CSS选择器,例如:

ie.Document.querySelector("button.advanced").Click

答案 1 :(得分:0)

试试这段代码......

(手绘类型未经测试的代码,因此可能有拼写错误。如果不起作用,请报告)

Sub ClickIEButton()
    Dim ie As Object
    Dim form As Variant, button As Variant

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.Naviagate ("http://whateverwebsite.com")
    While ie.ReadyState <> 4
        DoEvents
    Wend

    Set form = ie.Document.Body.GetElementsByTagName("form")(0) '<-- You may need to change this 0 to 1 or 2 or 3 etc....
    Set button = form.GetElementsByTagName("button")(0)  '<-- You may need to change this 0 to 1 or 2 or 3 etc....
    button.Click
End Sub