如何在 VBA 中单击网页上没有标签名称/ID 的按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12669012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:54:48  来源:igfitidea点击:

How to click a button, which does not have tag name/id, on web page in VBA?

excelvba

提问by Ujj

I have below code in a web page.

我在网页中有以下代码。

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

I want to click a button provided for search box. The web page code does not have tag name or ID.

我想单击为搜索框提供的按钮。网页代码没有标签名称或 ID。

I am using IE 8 browser.

我正在使用 IE 8 浏览器。

回答by QHarr

Try using a CSS selector such as:

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

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

回答by Pradeep Kumar

Try this code...

试试这个代码...

(Freehand typed untested code, so might have typos. Please report back if it doesn't work)

(徒手输入未经测试的代码,所以可能有错别字。如果它不起作用,请反馈)

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