比较一个集合中2个元素的索引 [英] Compare index of 2 elements in a collection

查看:101
本文介绍了比较一个集合中2个元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我有一些问题想出了一种方法来选择HTMLDocument中的页面中某个点下的元素。



在下面的代码示例中,您可以在注释中看到,我首先选择其中的一部分,它们遵守我的queryselector标准

  IEDoc.querySelectorAll(td [width ='100'] [class ='ListMainCent'] [rowSpan ='1'] [colSpan ='1'] )

在这个例子中,我在这个集合中有10个元素。

  MsgBox TypeName(IEDoc.querySelectorAll( td [width ='100'] [class ='ListMainCent'] [rowSpan ='1'] [colSpan ='1'])(2).ParentNode.ParentNode.ParentNode.ParentNode.ParentNode.ParentNode.ParentNode)' HTMLTable 

其中一些元素位于同一个表中。



你可以在这里看到包含所有表格的表格。

现在,我只想选择其中一些元素的innerHTML,而不是全部。要知道这10个元素中的哪一个是我感兴趣的标准是它在网页上的位置。我需要消息零件使用情况下的所有元素。只有一个表包含零件用法文本,所以我的想法是查看包含每个元素的表是否在表单集合中具有更高或更低的索引。
如果索引更高,我想要这个元素,否则我就放弃它。



我为此做了以下代码:


  1. 我将ID Bim 设置为10个元素中包含一个或多个
    的所有表。

     对于IEDoc.querySelectorAll中的每个元素(td [width ='100'] [class ='ListMainCent'] [rowSpan ='1'] [colSpan ='1'])'在这里查询所有10个数字,我们将在集合(表单)中找到它们各自的表并将其类设置为Bim。但由于某些数字在同一个表中,因此在流程结束时,我们不会有10个具有类名Bim的表。我们将只有类名为Bim的x表



    Element.ParentNode.ParentNode.ParentNode.ParentNode.ParentNode.ParentNode.ParentNode.Class =Bim



  2. / em>到包含文本的表格零件使用

     对于IEDoc.getElementsByClassName中的每个元素SectionHead)

    如果Element.innerHTML =零件用法,则

    'MsgBox TypeName(Element.ParentNode.ParentNode.ParentNode)'HTMLTable
    元素。 ParentNode.ParentNode.ParentNode.ID =Stop

    End If

    Next


  3. 使用ID Stop 检查包含Classname Bim 的表是否位于(= higher index)之下。对于符合第3点标准的表(实际上只有一个),我应用 IEDoc.querySelectorAll(td [width ='100'] [class ='ListMainCent'] [rowSpan ='1' ] [colSpan ='1']),这样我就可以获得包含的所有元素,更重要的是它们的innerHTML。

     对于IEDoc.getElementsByClassName(Bim)中的每个元素我们检查所有具有ClassnameBim的x表b 

    $ b如果Element .IndexInTheWholeForm> IEDoc.getElementById(Stop)。indexInTheWholeForm Then'并比较它们在(表单)集合中的索引是否高于ID为Stop的表格(这与检查网页中的元素是否低于(我们只希望在零件使用表下具有较高索引的元素)

    对于每个元素2在Element.querySelectorAll(td [width ='100'] [class =' ListMainCent'] [rowSpan ='1'] [colSpan ='1'])'现在我们在包含零件编号的表格中,我们将通过再次应用queryselectorall来查找它包含的所有零件编号,但这次只在这个特定的表中
    $ b $ array_parts2(iteration2)= Element.querySelectorAll(td [width ='100'] [class ='ListMainCent'] [rowSpan ='1'] [colSpan = '1'])(iteration2).innerHTML

    ActiveWorkbook.Worksheets(1).Cells(iteration2 + 1,19)= array_parts2(iteration2)

    iteration2 = iteration2 + 1
    Next

    End If
    Next


当然不起作用的是不存在的 TheWholeForm 属性。关于如何做到这一点的任何想法?



感谢您接触到此行:)

好吧,听起来像意想不到的,我想我找到了解决我自己问题的方法。我会向你确认,只要我有可能与我的同事一起运行,它就会起作用。
所以我从第一篇文章中保留第一点和第二点,并用以下代码替换第三点:

 对于我(0).getElementsByTagName(table)(i)。如果IEDoc.getElementsByTagName ID =StopThen 
index_Part_Usage = i
Position_Part_Usage = index + 1
退出对于
结束如果
下一个
'MsgBox Position_Part_Usage






对于i = 0 To IEDoc.getElementsByTagName(form)(0).getElementsByTagName(table)。length
如果IEDoc.getElementsByTagName(form)(0).getElementsByTagName(table)(i).className =BimThen
index = i
Position = index + 1
如果index> index_Part_Usage Then
For Each Element2 In IEDoc.getElementsByTagName(form)(0).getElementsByTagName(table)(i).querySelectorAll(td [width ='100'] [class ='ListMainCent'] [rowSpan ='1'] [colSpan ='1'])'现在我们在包含零件编号的表格中,我们将通过再次应用queryselectorall来查找它包含的所有零件编号,但这次只是在这个特定的表中

array_parts2(iteration2)= IEDoc.getElementsByTagName(form)(0).getElementsByTagName(table)(i).querySelectorAll(td [width ='100'] [class ='ListMainCent'] [rowSpan ='1'] [colSpan ='1'])(iteration2).innerHTML

ActiveWorkbook.Worksheets(1).Cells(iteration2 + 1,19 )= array_parts2(iteration2)

iteration2 = iteration2 + 1
Next
End If
End If
Next i


Issue : I have some issues figuring out a way to select elements in my HTMLDocument which are under a certain point in the page.

In the following code sample, as you can see in the comments, I first select a part of them which respect my queryselector criteria

IEDoc.querySelectorAll("td[width='100'][class='ListMainCent'][rowSpan='1'][colSpan='1']")

In this example I have 10 elements in this collection. Each of this element in contained in a table which is its parent on the 7th degree.

MsgBox TypeName(IEDoc.querySelectorAll("td[width='100'][class='ListMainCent'][rowSpan='1'][colSpan='1']")(2).ParentNode.ParentNode.ParentNode.ParentNode.ParentNode.ParentNode.ParentNode) ' HTMLTable

Some of those elements are in the same table.

You can see here the form which contains all the tables .

Now, the thing is that I want to select the innerHTML of some of those elements only and not all of them. The criterion to know if I one of those 10 elements interests me or not is it's position on the webpage. I want all the elements which are under the message Part Usage. There is only one table containing the Part Usage text and so my idea was to see if the table in which are contained each element has a higher or lower index in the "form" collection. If the index is higher I want this element, otherwise I discard it.

What I did for this is the following code :

  1. I set the ID Bim to all the tables containing one or more from the 10 elements.

     For Each Element In IEDoc.querySelectorAll("td[width='100'][class='ListMainCent'][rowSpan='1'][colSpan='1']") ' here for all of the 10 numbers found with the  queryselectorall we'll find their respective table in the collection (form) and set its Class as "Bim". But since some of the numbers are in the same table, we won't have 10 tables with a classname "Bim" at the end of the process. We'll have only x tables with the classname "Bim"
    
    
    
    Element.ParentNode.ParentNode.ParentNode.ParentNode.ParentNode.ParentNode.ParentNode.Class = "Bim"
    
    Next
    

  2. I set the ID Stop to the table containing the text Part Usage

    For Each Element In IEDoc.getElementsByClassName("SectionHead")
    
        If Element.innerHTML = "Part Usage" Then
    
           'MsgBox TypeName(Element.ParentNode.ParentNode.ParentNode)' HTMLTable
           Element.ParentNode.ParentNode.ParentNode.ID = "Stop"
    
        End If
    
     Next
    

  3. I check which tables with the Classname Bim are under (=higher index) the table with the ID Stop. For the table ( there is actually only one) matching the criterion of point 3 I apply IEDoc.querySelectorAll("td[width='100'][class='ListMainCent'][rowSpan='1'][colSpan='1']") inside of them so that I get all the elements in contains and more paricularly their innerHTML.

                                     For Each Element In IEDoc.getElementsByClassName("Bim") ' Here we check all the x tables which have the Classname "Bim"
    
    
        If Element.indexInTheWholeForm > IEDoc.getElementById("Stop").indexInTheWholeForm Then 'and compare somehow if their index in the (form) collection if higher than the table with the ID "Stop" ( this is similar to checking if the element if lower on the webpage in thic case) ( we only want the element which have a higher index aka under the Part Usage table)
    
               For Each Element2 In Element.querySelectorAll("td[width='100'][class='ListMainCent'][rowSpan='1'][colSpan='1']") ' Now we are in the table which contains the part numbers and we'll look for all the part numbers it contains by applying the queryselectorall again, but this time only in this specific table
    
                      array_parts2(iteration2) = Element.querySelectorAll("td[width='100'][class='ListMainCent'][rowSpan='1'][colSpan='1']")(iteration2).innerHTML
    
                     ActiveWorkbook.Worksheets(1).Cells(iteration2 + 1, 19) = array_parts2(iteration2)
    
                     iteration2 = iteration2 + 1
                Next
    
        End If
        Next
    

of course what doesn't work is the indexInTheWholeForm property which doesn't exist. Any ideas on how to do this ?

Thank for reaching that line :)

解决方案

Ok, so as unexpected as it sounds, I think I found a solution to my own question. I will confirm you that it works as soon as I have the possibility to run it with my colleague. So I keep point 1 and 2 from my initial post and I replaced point 3 with the following :

For i = 0 To IEDoc.getElementsByTagName("form")(0).getElementsByTagName("table").length 
          If IEDoc.getElementsByTagName("form")(0).getElementsByTagName("table")(i).ID = "Stop" Then
                index_Part_Usage = i
                Position_Part_Usage = index + 1
                Exit For
           End If
      Next
       'MsgBox Position_Part_Usage






      For i = 0 To IEDoc.getElementsByTagName("form")(0).getElementsByTagName("table").length
          If IEDoc.getElementsByTagName("form")(0).getElementsByTagName("table")(i).className = "Bim" Then
                index = i
                Position = index + 1
                If index > index_Part_Usage Then
                    For Each Element2 In IEDoc.getElementsByTagName("form")(0).getElementsByTagName("table")(i).querySelectorAll("td[width='100'][class='ListMainCent'][rowSpan='1'][colSpan='1']") ' Now we are in the table which contains the part numbers and we'll look for all the part numbers it contains by applying the queryselectorall again, but this time only in this specific table

                        array_parts2(iteration2) = IEDoc.getElementsByTagName("form")(0).getElementsByTagName("table")(i).querySelectorAll("td[width='100'][class='ListMainCent'][rowSpan='1'][colSpan='1']")(iteration2).innerHTML

                         ActiveWorkbook.Worksheets(1).Cells(iteration2 + 1, 19) = array_parts2(iteration2)

                        iteration2 = iteration2 + 1
                    Next
                End If
           End If
      Next i

这篇关于比较一个集合中2个元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆