問題:ページ内の特定のポイントにある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
これらの要素のいくつかは同じ表にあります。
ここでは、それらの要素のうちの一部のinnerHTMLのみを選択し、それらのすべてではないということです。私がこれらの10要素のうちの1つが私に興味を持っているかどうかを知る基準は、ウェブページ上のポジションです。私はメッセージの下にあるすべての要素をご希望ですPart Usage。 Part Usageのテキストを含む表が1つしかないので、私の考えは、各要素が含まれている表に "form"コレクションのインデックスが高いか低いかを調べることでした。 インデックスが高い場合はこの要素が必要です。それ以外の場合は破棄します。私は10個の要素から1つ以上の を含むすべてのテーブルにID ビムを設定
:私はこのためにした何
は次のコードです。
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
私は(=高屈折率)の下にあるクラス名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
を含むテーブルに停止 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
動作しませんどのようなコースの
は存在しないindexInTheWholeFormプロパティです。これを行う方法に関するアイデア?
:)
'getElementById'はIDがページ全体にわたって一意であることを想定しているので、1つの要素を返すことになっています。同じIDを複数の要素に設定している場合、それは良い考えではありません。 –
もちろん、あなたは正しいです。私はこの間違いに気付かなかった。代わりに同じメソッドを使用し、その名前を使用すると、このインデックスの問題を修正する方法はありますか? – Seb