如何使用Excel VBA单击href [英] How to Click an href using an Excel VBA
问题描述
我是VBA的新手,并且对如何单击Internet Explorer中的href链接有疑问.在源页面上有多个href.我从未遇到过,给我带来了很大的困难!我在这个网站上寻找答案,但决定在这里提问.
I am very new to VBA and had a question regarding how to click an href link in Internet Explorer. There are multiple href's on the source page. I have never encountered this and it has been giving me a hard time! I have looked on this website searching for answers but decided to ask here.
在下面,我列出了我拥有的代码,直到遇到问题为止,以及Internet Explorer上的源代码.
Below I have listed the code I have, up to the point where I encounter the problem, as well as the Source Code on Internet Explorer.
我注释掉了我尝试过的内容,并列出了收到的错误.
I commented out what I have tried and listed the error I received.
以下代码:
Sub ()
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
User = "User"
Pwd = "Pwd"
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "URL.com"
IE.Navigate URL
Do While IE.ReadyState <> 4
DoEvents
Loop
IE.Document.getElementById("txtUsername").Value = User
IE.Document.getElementById("txtPassword").Value = Pwd
IE.Document.getElementById("btnSubmit").Click
'IE.getElementByClassName("txtTerms").Click - Runtime Error 438
'IE.getElementByTagName("Claims Management").Click - Runtime Error 438
'Set HREF = IE.Document.getElementsByClassName("txtTerms")
'For Each HREF In IE.Document.getElementsByTagName("Claims").Click - No error occurs, nothing happens.
End Sub
Internet Explorer源代码:
Internet Explorer Source Code:
<table id="tblContent">
<tr>
<td class="txtTerms"><a href='href url 1'>Claims</a>
<br>Download<br>Create<br><a class='terms' href='href url 2'
target='terms'>Terms</a><br><br></td>
</tr>
我的问题是,如何使VBA仅单击"href url 1"?
My question would be, how to get VBA to click only on 'href url 1'?
让我知道是否需要其他信息.我为自己的VBA水平道歉,但我很高兴了解更多!
Let me know if any additional information is needed. I apologize for my level of VBA but I am excited to learn more!
感谢您的帮助!
推荐答案
在HTML中, href
是< a>
(链接)类型的属性,其中包含绝对或相对路径.例如:
In HTML, href
is a property of the type <a>
(link) which contains an absolute or relative path.
For example:
<a href="/questions/">Questions</a>
...将显示为问题",如果单击它,将带您到 www.stackoverflow.com/questions/.请注意,由于路径是相对的,因此已自动添加"www.stackoverflow.com".
... will show as "Questions" and, if you click it, will bring you to www.stackoverflow.com/questions/. Note that "www.stackoverflow.com" has been added automatically since the path is relative.
<a href="https://www.facebook.com">Facebook</a>
...将显示为"Facebook",如果单击它,将带您到 www.facebook.com .在这种情况下,路径是绝对的.
... will show as "Facebook" and, if you click it, will bring you to www.facebook.com. In this case, the path is absolute.
尽管您的HTML代码不完整,但我想您要导航的所有链接都包含在 id ="tblContent"
的表
中.如果是这样,那么您可以获取该表中的所有链接( tagName =='a')
并将值存储在集合中:
Although your HTML code is incomplete, I guess that all the links you want to navigate are contained in the table
having id="tblContent"
. If that's the case, then you can get all the links (tagName == 'a')
in that table and store the values in a collection:
Dim allHREFs As New Collection
Set allLinks = IE.Document.getElementById("tblContent").getElementsByTagName("a")
For Each link In allLinks
allHREFs.Add link.href
Next link
然后,您可以决定导航它们并做您必须做的一件事情:
You can then decide to navigate them and do what you have to do one by one:
For j = 1 To allHREFs.Count
IE.Navigate URL + allHREFs(j) '<-- I'm assuming hrefs are relative.
'do your stuff here
Next href
这篇关于如何使用Excel VBA单击href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!