2016-01-20 17 views
0

我正在修改相當廣泛的VBA Web scraper,並尋找一些關於如何在沒有指定實際id時通過id獲取元素的建議。VBA - 在沒有指定ID時使用getElementById

<ul class="rtUL"> 
    <li class="rtLI"><div class="rtMid"> 
    <span class="rtSp"></span><span class="rtPlus"></span> 
    <a class="rtIn" href="/mhpviewer.aspx?FID=CSTAT">Claim Status</a> 
    </div></li><li class="rtLI"><div class="rtMid"> 
    <span class="rtSp"></span><span class="rtPlus"></span> 
    <a class="rtIn" href="/mhpviewer.aspx?FID=EVER">Eligibility Verification</a> 

我需要得到的元素是<li>包含「資格驗證」。有沒有辦法讓孩子的元素或內部的HTML?

+0

請問[這個線程](http://stackoverflow.com/questions/11805389/getting-data-from-html-source-in-vba-excel)有幫助嗎?或者,也許[這一個](http://stackoverflow.com/questions/12344559/how-to-use-getelementbyid-and-copy-inner-html-vba)? – BruceWayne

+0

我正在處理修改數百個Web頁面的代碼,並試圖學習所有獲取這些元素的各種方法,因爲它可能更容易創建變量,即它們都將需要相同類型的輸入,而不是尋找特定的id或內部的HTML文本,我試圖找到一種方法來看看每個李在無序列表中,所以我可以儘量減少代碼。這可能是不可能的,但有人可能會有一些提示。 – justkrys

回答

2

我假設您使用的是InternetExplorer.Application(版本8及以上版本),已設法檢索該頁面,格式與您的問題相同。

Set Data = IE.Document.querySelectorAll("li>div>a") 
'looks for tag `a` inside tag `div` inside tag `li` 

For Each oA In Data 
    If oA.InnerHTML = "Eligibility Verification" Then 
     Set oLI = oA.ParentNode.ParentNode 
     Debug.Print oLI.InnerHTML 
    End If 
Next oA 
+0

每當我嘗試使用除Debug.Print之外的任何其他功能時,它都會崩潰我的Web瀏覽器IEv11。嵌套元素提示是一個偉大的,但我認爲會導致我在我所希望的方向。謝謝! – justkrys

相關問題