2016-11-18 6 views
0

問題:ページ内の特定のポイントにあるHTMLDocumentの要素を選択する方法がいくつかあります。次のコードサンプルでコレクション内の2つの要素のインデックスを比較します。

、あなたがコメントで見ることができるように、私が最初にこの例では、私のqueryselector基準

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

を尊重し、それらの一部を選択し、私はこのコレクションの10個の要素を持っています。この要素のそれぞれは、7階の親であるテーブルに含まれています。

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

これらの要素のいくつかは同じ表にあります。

すべてのテーブルが含まれているフォームをご覧ください。the form which contains all the tables

ここでは、それらの要素のうちの一部のinnerHTMLのみを選択し、それらのすべてではないということです。私がこれらの10要素のうちの1つが私に興味を持っているかどうかを知る基準は、ウェブページ上のポジションです。私はメッセージの下にあるすべての要素をご希望ですPart UsagePart Usageのテキストを含む表が1つしかないので、私の考えは、各要素が含まれている表に "form"コレクションのインデックスが高いか低いかを調べることでした。 インデックスが高い場合はこの要素が必要です。それ以外の場合は破棄します。私は10個の要素から1つ以上の を含むすべてのテーブルにID ビムを設定

  1. :私はこのためにした何

    次のコードです。

    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. 私は(=高屈折率)の下にあるクラス名Bimのとどのテーブルチェックテキストパート使用

    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. を含むテーブルに停止 ID を設定しますID のテーブルを停止します。ポイント3の基準にマッチするテーブル(実際には1つだけ)のために、私はIEDoc.querySelectorAll("td[width='100'][class='ListMainCent'][rowSpan='1'][colSpan='1']")を内部に適用して、すべての要素を包含し、さらには特に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 
    
  4. 動作しませんどのようなコースの

は存在しないindexInTheWholeFormプロパティです。これを行う方法に関するアイデア?

:)

+2

'getElementById'はIDがページ全体にわたって一意であることを想定しているので、1つの要素を返すことになっています。同じIDを複数の要素に設定している場合、それは良い考えではありません。 –

+0

もちろん、あなたは正しいです。私はこの間違いに気付かなかった。代わりに同じメソッドを使用し、その名前を使用すると、このインデックスの問題を修正する方法はありますか? – Seb

答えて

0

テストされていないそのラインに到達するための感謝が、私はこのような何かをするだろう(私が正しくあなたを理解仮定)

Sub Tester() 

    Const S_MATCH As String = "td[width='100'][class='ListMainCent'][rowSpan='1'][colSpan='1']" 

    Dim e, tbl, bHit As Boolean 

    '... 
    'load page etc 
    '... 


    'get all the matching rows and cycle though them 
    For Each e In IEDoc.querySelectorAll(S_MATCH) 

     'did we get to the table of interest yet? 
     If Not bHit Then 
      Set tbl = e.ParentNode.ParentNode.ParentNode.ParentNode. _ 
         ParentNode.ParentNode.ParentNode 
      If IsPartUsageTable(tbl) Then bHit = True 
     End If 

     If bHit Then 
      'we reached the table of interest, so 
      ' do something with e 
     End If 

    Next 

End Sub 

Function IsPartUsageTable(tbl) As Boolean 
    Dim e, rv As Boolean 
    For Each e In tbl.getElementsByClassName("SectionHead") 
     If Element.innerHTML = "Part Usage" Then 
      rv = True 
      Exit For 
     End If 
    Next 
    IsPartUsageTable = rv 
End Function 
+0

ちょっとティム、あなたの解決に感謝しますが、それは私が探していたものではありません。 'IEDoc.querySelectorAll(S_MATCH)'では、要素の束を選択し、それらの要素のそれぞれは7番目の親度としてテーブルを持っています。それらのテーブルは形をしています(写真参照)。次に、それらの表が_Part Usage_テキストを含む表よりも高いか低いかを(インデックスの点で)確認する必要があります。テーブルのインデックスが高い(リストの下にある)場合は、それらの要素を保持します。また、私はIDの問題の期間に私のコードを編集し、私はクラスに置き換えました。 – Seb

+0

OK私のコードでは、 "Part Usage"テーブル内にあるかどうかをテストしています。そのテーブルを超えて*これらの行だけを処理するように調整することができます。 –

0

[OK]を、それは音として、私は私が見つけたと思う予期しないように私自身の質問に対する解決策。私は、私が同僚とそれを実行する可能性があるとすぐにそれが動作することを確認します。 は、だから私は私の最初のポストからポイント1と2を維持し、私はポイント3を交換し、次の

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 
関連する問題