VBA getElementById with dynamic ID

老子叫甜甜 提交于 2019-12-02 04:50:56

问题


I've been searching this whole forum, msdn and specialised tutorials and I can't find the answer for VBA: How can I make the getElementById work in an access VBA module where the id to find is dynamic?

Let's see the html code:

<DIV id=rowToolTipContainer>
<DIV class=contactsCard id=resultsTooltip1122286Contents style="DISPLAY: none">
<TABLE class="shadow-box tooltip">
<TBODY>

And how I'm trying to find it:

Dim ResultDIV As HTMLDivElement   
Set ResultDIV = HTMLDoc.getElementById("resultsTooltip*")

Let me say the html returned has a different id (the numbers change) depending on each result so the id for each DIV is always:

id=resultsTooltipxxxxxxxContents where xxxxxxx are always different numbers

Any help would be highly appreciated.


回答1:


Try something like this one:

Dim ContainerDiv As HTMLDivElement, ResultDIV As HTMLDivElement

Set ContainerDiv = HTMLDoc.getElementById("rowToolTipContainer")
For Each ResultDIV In ContainerDiv.GetElementsByTagName("div")
    If ResultDIV.ID Like "resultsTooltip*Contents" Then

        '' What do you want to do here?

        Exit For
    End If
Next



回答2:


  1. Identify the closest parent tag that always contains the ID (manually, by looking at your HTML).
  2. Enumerate all descendant <div>s of that tag, testing their ID property with Like.


来源:https://stackoverflow.com/questions/11048940/vba-getelementbyid-with-dynamic-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!