簡體   English   中英

當有多個具有相同名稱的Classname,tagname和屬性時,無法使用VBA提取文本

[英]Unable to extract Text using VBA, when there are multiple Classname , tagname and attributes with same name

我試圖從IE瀏覽器中提取“ span”類下的內部文本,但遇到了問題。 以下是HTML代碼

第一個html代碼段

<div class="SectionStart" style="display: block;" oraload="oraDisplayNone(item,'newDev','Y','=')" oraInitDisplayStyle="block">
  <label oramdlabel="CM_SN">Serial Number</label>
<!--style tag has been added-->
  <span class="oraDefault" style="left: -5px; top: 4.5px; position: relative;" orafield="MSerialNumber" oraType="string;precision:20" oraErrorElement="mSerialNumber">G4A001</span>
</div>

第2個HTML代碼段

<div class="SectionStart">
  <label oramdlabel="CM_SN">Serial Number:</label>
<!--style tag has been added-->
  <span class="oraDefault" style="left: -5px; top: 4.5px; position: relative;" orafield="MSerialNumber" oraType="string;precision:20" oraErrorElement="mSerialNumber">E5W807</span>

</div>

這里的問題是根據下面的代碼,嘗試獲取第二個值“ E5W807”時,我僅獲取了不需要的值“ G4A001”。

請指導如何解決僅提取文本“ E5W807”的問題。

 Set objSubCollec = objCollection(0).contentWindow.document.getElementById("Page")
    Set objElement = objSubCollec.contentWindow.document.getElementById("Frame_4")
    objElement.Focus
   Set objElement1 = objElement.contentWindow.document
   Set elm2 = objElement1.getElementsByTagName("span")

          For Each e1 In elm2
              If e1.getAttribute("orafield") = "MSerialNumber" Then
                 temp = e1.innerText
                'MsgBox temp
                Worksheets("Sheet1").Range("B6") = temp
        Exit For
    End If
    Next

也許試試這個:

它將連續從B6打印所有找到的號碼。

Set objSubCollec = objCollection(0).contentWindow.document.getElementById("Page")
Set objElement = objSubCollec.contentWindow.document.getElementById("Frame_4")
objElement.Focus
Set objElement1 = objElement.contentWindow.document
Set elm2 = objElement1.getElementsByTagName("span")


i = 6
For Each e1 In elm2
    If e1.getAttribute("orafield") = "MSerialNumber" Then
       temp = e1.innerText
       Worksheets("Sheet1").Range("B & i") = temp
       i = i + 1
    End If
Next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM