如何使用 Excel VBA 单击网页上的链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24638533/
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
How do I click a link on a web page using Excel VBA?
提问by Mutuelinvestor
I'm writing VBA code to obtain a ticker symbol from a user, navigate to a website, input the ticker symbol and click the appropriate link.
我正在编写 VBA 代码以从用户那里获取股票代码,导航到网站,输入股票代码并单击相应的链接。
I researched this StackOverflow question and response, however, I don't have an innertext value to utilize.
我研究了这个StackOverflow question and response,但是,我没有可以使用的内文值。
My VBA code:
我的 VBA 代码:
Sub clicklick()
Dim ie As Object
Dim form As Variant, button As Variant
Set ie = CreateObject("InternetExplorer.Application")
ticker = InputBox("Enter Ticker Symbol: ")
With ie
.Visible = True
.Navigate ("http://www.SITE_URL.com")
While ie.ReadyState <> 4
DoEvents
Wend
ie.document.getElementsbyName("sSrchTerm").Item.innertext = ticker
End With
End Sub
The link appears as follows in the page source:
该链接在页面源中显示如下:
<a class="hqt_button" href="javascript:void(0): onclick=HeaderBox.trySubmit()"></a>
The element doesn't seem to have a name, innertext or id. How can I click the link?
该元素似乎没有名称、内部文本或 ID。如何点击链接?
EDIT:
编辑:
Set Link = ie.document.getElementsByTagName("a")
For Each l In Link
If Link.classname = "hqt_button" Then
Link.Click
Exit For
End If
Next l
回答by David Zemens
Try getting the collection of anchor tags, with:
尝试获取锚标签的集合,使用:
GetElementsByTagName("a")
Then, iterate that collection using as much logic as you can to ensure you're clicking the right button.
然后,使用尽可能多的逻辑迭代该集合,以确保您单击了正确的按钮。
For each l in ie.document.getElementsByTagName("a")
If l.ClassName = "hqt_button" Then
l.Click
Exit For
Next
If there are multiple anchors with the same classname, you could do:
如果有多个具有相同类名的锚点,您可以执行以下操作:
If l.ClassName = "hqt_button" AND l.Href = ""javascript:void(0): onclick=HeaderBox.trySubmit()" Then
l.Click
Exit For
Next
Alternatively
或者
If you are using IE9+ you could use the GetElementsByClassName
method.
如果您使用的是 IE9+,则可以使用该GetElementsByClassName
方法。
GetElementsByClassName("hqt_button")
回答by QHarr
You could also use a CSS selectorof
你也可以使用一个CSS选择器的
a.hqt_button[href='javascript:void(0): onclick=HeaderBox.trySubmit()']
The "."
means className. []
encloses an attribute. The above says className hqt_button
with attribute href
with value 'javascript:void(0): onclick=HeaderBox.trySubmit()'
, inside of an a
tag.
该"."
机构的className。[]
包含一个属性。上面说 classNamehqt_button
带有href
value属性'javascript:void(0): onclick=HeaderBox.trySubmit()'
,在a
标签内。
CSS query:
CSS查询:
Syntax in VBA:
VBA 中的语法:
CSS selectors are applied via the .querySelector
method of the HTMLDocument.
CSS 选择器通过.querySelector
HTMLDocument的方法应用。
ie.document.querySelector("a.hqt_button[href='javascript:void(0): onclick=HeaderBox.trySubmit()']").Click