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

时间:2018-01-30 12:27:38

标签: html vba

我在网页上有一个列表(一个非常重复的工作任务),我需要从中选择一些元素,然后按一个按钮来选择它们(按下按钮的部分不是问题,因为我知道怎么做)。 我发现我可以选择的选项是包含/表示表的ID的Children / CHildren节点。这与MACRO相比似乎相当复杂。 有没有办法做一些像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"

2 个答案:

答案 0 :(得分:1)

您可以选择站点中的所有DIV标记,然后检查其内部HTML是否包含文本:title =&#34;。  这样的事情:

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部分。

答案 1 :(得分:0)

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

下面的代码循环遍历一个名为的类中的项目:“使用类名替换它,直到你需要的那个循环”。 (我认为项目是你的孩子,但我不确定术语,现在无法验证。)

首先初始化对象和变量:

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

或者:

Set elements0d = ie.document.getElementsByName("substitutethis with the element names untill you loop through the one you need")

Set elements0d = ie.document.getElementsById("substitutethis with the element Id's untill you loop through the one you need")

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

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