繁体   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