2017-08-15 96 views
0

私は初心者からExcel vbaで掻き集めて助けが必要です。xpathでhtml要素を参照しているvba

要素を参照しようとしています。 IDがある場合はgetElementByIDを使用できますが、IDがないことがあります。私はgetElementByClassNameを使うことができますが、時には同じクラスの要素が多すぎます。

xpathで要素を参照する方法はありますか?

は(個人情報は非常に私たちは、これはhtmlです言わせてありますので、私は実際にウェブサイトを投稿することはできません)

<!DOCTYPE html> 
<html> 
<body> 

<a href="https://google.com">Link</a> 

</body> 
</html> 

ie.document.getElementByXPathのようなものがあります。(/ HTML /ボディ/ a).click? 私はウェブ全体を検索しており、そのトピックについては何も見つかりませんでした。

+0

私はあなたが属性で要素を選択することを指していると思うかもしれません。たとえば、要素を 'href == 'google.com'のどこに置くかなどです。私は正しいですか? –

+0

HTMLが有効なXHTMLであれば、有効なXMLであり、必要な機能を提供するMSXMLライブラリで処理できます。 –

+0

xpathではなく、 'queryselector [all]'はあなたが求めているものに近いです:https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector –

答えて

0

これが答えここ

となるものではありませんが、あなたにいくつかのアイデア

Sub google() 

    ' add reference: Microsoft XML v6.0 

    Const url = "https://www.google.co.in" 

    Dim http As New XMLHTTP60 
    Dim html As New HTMLDocument 

    http.Open "GET", url, False 
    http.Send 
    html.body.innerHTML = http.responseText 

    Dim elem As Object 
    Set elem = html.getElementsByClassName("ctr-p")    ' HTMLElementCollection 
    Debug.Print elem.Length 

    Set elem = html.getElementsByClassName("ctr-p")("viewport") ' HTMLDivElement <div class="ctr-p" id="viewport"> 
    Debug.Print elem.Children.Length 


    Dim aaa As Object 
    Set aaa = elem.getElementsByTagName("div")("hplogo")   ' HTMLDivElement 
    Debug.Print aaa.Children.Length 
    Debug.Print aaa.outerHTML 

End Sub 

を与える可能性があり潜水艦のカップルです。

' add references Microsoft HTML Object Library 
'     Microsoft Internet Controls 

Sub ieGoogle() 

    Const url = "https://www.google.co.in" 

    Dim iE As InternetExplorer 
    Set iE = New InternetExplorer 

    iE.Navigate url 
    iE.Visible = True 

    Do While iE.ReadyState <> 4: DoEvents: Loop 

    Dim doc As HTMLDocument 
    Set doc = iE.Document 

    Debug.Print doc.ChildNodes.Length       ' DOMChildrenCollection 
    Debug.Print doc.ChildNodes(1).ChildNodes.Item(0).nodeName ' HEAD 
    Debug.Print doc.ChildNodes(1).ChildNodes.Item(1).nodeName ' BODY 


    ' for querySelector arguments see: https://www.w3schools.com/cssref/css_selectors.asp 

    Dim elm As HTMLInputElement 
    Set elm = doc.querySelector("*")      ' all elements 

    Debug.Print Left(elm.outerHTML, 40) 
    Set elm = doc.querySelector("div.ctr-p#viewport") ' <div class="ctr-p" id="viewport"> 
    Debug.Print Left(elm.outerHTML, 40) 
    Set elm = doc.querySelector(".ctr-p#viewport")  ' <div class="ctr-p" id="viewport"> 

    Debug.Print Left(elm.outerHTML, 40) 
    Debug.Print elm.ChildNodes.Length 
    Debug.Print elm.Children.Length 


    Set elm = doc.querySelector("#viewport")    ' id="viewport" 
    Debug.Print Left(elm.outerHTML, 40) 


    Debug.Print elm.ID 


    Dim elem As HTMLInputElement 
    Set elem = doc.getElementsByClassName("ctr-p")("viewport") 



    Debug.Print elem.Children.Length 

    Dim aaa As Object 
    Set aaa = elem.getElementsByTagName("div")("hplogo") 
    Debug.Print aaa.Children.Length 
    Debug.Print aaa.outerHTML 

    iE.Quit 
    Set iE = Nothing 
End Sub 
関連する問題