Html 在ID子节点/子节点中搜索特定值

Html 在ID子节点/子节点中搜索特定值,html,vba,Html,Vba,我在网页上有一个列表(工作中的一个非常重复的任务),我需要从中选择一些元素,然后按下按钮来选择它们(按下按钮的部分不是问题,因为我知道怎么做)。 我发现我可以选择的选项是包含/表示表的ID的子节点/子节点。对于宏来说,这似乎相当复杂。 有没有办法像getElementbyID那样调用/搜索“标题”?因为它似乎是每个选项的唯一值? 父html代码如下所示: class="mstrListBlockItem" title="2018 KW 05" style="margin-top: 0px;" c

我在网页上有一个列表(工作中的一个非常重复的任务),我需要从中选择一些元素,然后按下按钮来选择它们(按下按钮的部分不是问题,因为我知道怎么做)。 我发现我可以选择的选项是包含/表示表的ID的子节点/子节点。对于宏来说,这似乎相当复杂。 有没有办法像getElementbyID那样调用/搜索“标题”?因为它似乎是每个选项的唯一值?

父html代码如下所示:

class="mstrListBlockItem" title="2018 KW 05" style="margin-top: 0px;"
class="mstrBGIcon_ae mstrListBlockItemName" style="background-position: 2px 50%; padding-left: 23px;">2018&nbsp;KW&nbsp;05</div><div class="mstrListBlockItem" title="2018 KW 04"><div class="mstrBGIcon_ae mstrListBlockItemName" style="background-position: 2px 50%; padding-left: 23px;">2018&nbsp;KW&nbsp;04</div></div><div class="mstrListBlockItem" title="2018 KW 03"><div class="mstrBGIcon_ae mstrListBlockItemName" style="background-position: 2px 50%; padding-left: 23px;">2018&nbsp;KW&nbsp;03</div></div><div class="mstrListBlockItem" title="2018 KW 02"
class=“mstrListBlockItem”title=“2018千瓦05”style=“利润率顶部:0px

class=“mstrBGIcon\u ae mstrListBlockItemName”style=“背景位置:2px 50%;左侧填充:23px;”>2018 KW 052018 KW 042018 KW 03您可以选择站点中的所有DIV标记,然后检查其内部HTML,如果它们包含文本:title=“”。 诸如此类:

Dim oDivs As IHTMLElementCollection
Dim dv As IHTMLElement

Set oDivs = iBody.getElementsByTagName("div")
For Each dv In oDivs
    If InStr(dv.innerHTML, "title=""") Then
        'some code here
    End If
Next dv

其中,iBody是HTML的主体,您的按钮应该位于其中。

作为一般解决方案,您可以使用元素的不同“属性”来查找您需要单击的元素。属性如“.innertext”、“.Children”,将列出更完整的文档

下面的代码循环遍历名为“substitutethis和classnames的类中的项,直到您循环遍历所需的项”。(我认为项就是您所指的子项,但我不确定术语,目前无法验证。)

首先初始化对象和变量:

Dim ie As Object
Dim objelement As Object
Set ie = CreateObject("InternetExplorer.Application")
        With ie
        .Visible = True
        .navigate "https://the.websitethatcontainsyour.list"

        'wait until first page loads
        Do Until .readyState = 4
            DoEvents
        Loop

'Start looping through the elements:
Set elements0d = ie.document.getElementsByClassName("substitutethis with the classnames untill you loop through the one you need")
For Each item In elements0d
    MsgBox (elements0d.item.className & elements0d.item.innerText)                
    If elements0d.item.innerText = ThisWorkbook.Worksheets("somesheet which value your entry in the list needs to equal").Range("B2").Value & " (FR)" Then
        MsgBox ("already logged in")
        logged_in = True                    
    End If
Next
通过列表中的元素循环的另一种方法是:

Set elements0d = ie.document.getElementsByTagName("substitutethis with the classnames untill you loop through the one you need")
For Each item In elements0d
    MsgBox (elements0d.item.className & elements0d.item.innerText)                
    If elements0d.item.innerText = ThisWorkbook.Worksheets("somesheet which value your entry in the list needs to equal").Range("B2").Value & " (FR)" Then
        MsgBox ("already logged in")
        logged_in = True                    
    End If
Next
或:

应用于您的数据,上述解决方案可能如下所示:

Dim ie As Object
Dim objelement As Object
Set ie = CreateObject("InternetExplorer.Application")
        With ie
        .Visible = True
        .navigate "https://the.websitethatcontainsyour.list"

        'wait until first page loads
        Do Until .readyState = 4
            DoEvents
        Loop

dim seaching_through_classes(0 to 10) as string
seaching_through_classes(0) = "mstrListBlockItem"
seaching_through_classes(1) = "mstrBGIcon_ae mstrListBlockItemName"

dim searchterm as string
searchterm = "2018 KW 05"

for loop_through_classes = 0 to 1
    'Start looping through the elements:
    Set elements0d = ie.document.getElementsByClassName(search_through_classes(loop_through_classes))
    For Each item In elements0d
        MsgBox (elements0d.item.className & elements0d.item.title)                
        If elements0d.item.innerText = "2018 KW 05" Then
            MsgBox ("Found title: " + "2018 KW 05")
        End If
    Next
Next loop_through_classes

非常感谢您的回复。他们都非常有用!!
Dim ie As Object
Dim objelement As Object
Set ie = CreateObject("InternetExplorer.Application")
        With ie
        .Visible = True
        .navigate "https://the.websitethatcontainsyour.list"

        'wait until first page loads
        Do Until .readyState = 4
            DoEvents
        Loop

dim seaching_through_classes(0 to 10) as string
seaching_through_classes(0) = "mstrListBlockItem"
seaching_through_classes(1) = "mstrBGIcon_ae mstrListBlockItemName"

dim searchterm as string
searchterm = "2018 KW 05"

for loop_through_classes = 0 to 1
    'Start looping through the elements:
    Set elements0d = ie.document.getElementsByClassName(search_through_classes(loop_through_classes))
    For Each item In elements0d
        MsgBox (elements0d.item.className & elements0d.item.title)                
        If elements0d.item.innerText = "2018 KW 05" Then
            MsgBox ("Found title: " + "2018 KW 05")
        End If
    Next
Next loop_through_classes