IE 自动化 VBA Excel 按标题查找元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39573176/
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-12 11:18:34  来源:igfitidea点击:

IE automation VBA Excel find element by title

htmlexcelvbaexcel-vba

提问by Dextrious

I am trying to get some information from a webpage using an excel macro, VBA.

我正在尝试使用 Excel 宏 VBA 从网页中获取一些信息。

I have the InternetExplorer.document object, but don't find a way to locate the exact info that I need.

我有 InternetExplorer.document 对象,但找不到找到我需要的确切信息的方法。

The HTML part of what I am looking for looks like this:

我正在寻找的 HTML 部分如下所示:

<a title="BE xxx.xxx.xxx - straat 70 - 3630 - Maasmechelen" class="leftalign floatleft" href="Page_companydetail.asp?vat=xxxxxxxxx" target="_blank">

 Oprichter van een onderneming natuurlijk persoon BE xxx.xxx.xxx 

 Oprichter van een onderneming natuurlijk 人 BE xxx.xxx.xxx 

The information I want is the title here. I tried a lot of things, but can't manage to single this element out and get the title.

我想要的信息是这里的标题。我尝试了很多东西,但无法设法将这个元素单独列出并获得标题。

So 1. Is there a way to get elements by title (Title starts with BE), or by class sinds this information is the only one on the page that has class "leftalign floatleft"

所以 1. 有没有办法按标题(标题以 BE 开头)或按类获取元素,此信息是页面上唯一具有“leftalign floatleft”类的信息

采纳答案by MikeD

Yes ... knowing that the title is just another attribute of the <a>tag, you can cycle through all elements of concern using e.g.

是的......知道标题只是<a>标签的另一个属性,您可以使用例如循环浏览所有关注的元素

HtmlDocument.GetElementsByTagName(String)method

HtmlDocument.GetElementsByTagName(String)方法

and use the

并使用

HtmlElement.GetAttribute(String)method

HtmlElement.GetAttribute(String)方法

to see if a title attribute exists and what's the value of it

查看 title 属性是否存在以及它的值是什么

see Reading Web Pages using Excel VBAfor some more information

有关更多信息,请参阅使用 Excel VBA 阅读网页

回答by Ryan Wildry

Something like this should work, I'm assuming you already have a pointer to the IE or Document Object.

这样的事情应该可以工作,我假设您已经有了指向 IE 或文档对象的指针。

Public Sub getTitleElement()
    Dim myElement As Object

    'Assuming you already have IE object/Document
    Set myElement = IE.Document.getElementsByClassName("leftalign floatleft")(0)

    Debug.Print myElement.Title

End Sub