首页 文章

使用VBA获取没有类名的span值

提问于
浏览
3

我坚持这个,

VBA代码:

Set html = IE.document

Set elements = html.getElementsByClassName("dropdown dropdown-toggle editable-field-title-wrapper")

Dim count As Long
Dim erow As Long
count = 0
For Each element In elements
If element.className = "dropdown dropdown-toggle editable-field-title- wrapper" Then
erow = Sheets("Exec").Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Sheets("Exec").Cells(erow, 1) = html.getElementsByTagName("span") 
(count).innerText

count = count + 1
End If
Next element

HTML代码:

<span data-link="class{:~getPriorityLabel(fieldValue) ? 'editable-field label-priority-color-' + ~getPriorityLabel(fieldValue) : 'editable-field'}" class="editable-field label-priority-color-high">
    <span class="dropdown dropdown-toggle editable-field-title-wrapper" data-toggle="dropdown">
      <script type="jsv#432^"></script><script type="jsv#814_"></script>
      <span class="editable-field-title">Priority:</span>
      <script type="jsv/814_"></script><script type="jsv/432^"></script>
      <span data-link="html{>~isUndefined(fieldValue) ? 'None' : ~capitalize(~getPriorityLabel(fieldValue))}">**High**</span>
      <span data-link="visible{:state != 'saving'}" style="display: block;">
        <span class="caret"></span>
      </span>
      <span data-link="visible{:state == 'saving'}" style="display: none;">
        <script type="jsv#815_"></script><i class="icon-spinner"></i><script type="jsv/815_"></script>
      </span>
    </span>
    <ul class="dropdown-menu priority-dropdown pull-right"><li data-link="visible{:fieldValue}" style="display: list-item;">
        <a class="editable-field-commit" data-value="-1" href="#" tabindex="0">None</a>
      </li><li>
        <a class="editable-field-commit" data-value="0.07438533240156266" href="#" tabindex="0">Low</a>
      </li><li>
        <a class="editable-field-commit" data-value="0.49243981401033865" href="#" tabindex="0">Medium</a>
      </li><li>
        <a class="editable-field-commit" data-value="0.9642772538736262" href="#" tabindex="0">High</a>
      </li></ul>
    <div class="alert alert-error editable-field-message-open-left editable-field-error-message" data-link="html{>errorMessage} visible{:errorMessage}" style="display: none;"></div>
    <div class="alert alert-info editable-field-conflict-message" data-link="html{>conflictMessage} visible{:conflictMessage}" style="display: none;"></div>
  </span>

请在我的HTML代码中找到 High ,我想要使用我的Excel VBA代码(High),请帮帮我 . !!

请在我的HTML代码中找到 High ,我想要使用我的Excel VBA代码(高),请帮帮我 . !!

2 回答

  • 0

    Using the traditional way.

    Change inside for loop ===>
    html.getElementsByTagName("span")(count).innerText To
    element.getElementsByTagName("span")(count).innerText

    Set html = IE.document
    
    Set elements = html.getElementsByClassName("dropdown dropdown-toggle editable-field-title-wrapper")
    
    Dim count As Long
    Dim erow As Long
    count = 0
    For Each element In elements
    If element.className = "dropdown dropdown-toggle editable-field-title- wrapper" Then
    erow = Sheets("Exec").Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
    Sheets("Exec").Cells(erow, 1) = html.getElementsByTagName("span")(count).innerText '--> change here
    
    count = count + 1
    End If
    Next element
    
  • 0

    如果你的意思是“ High ”,那么你可以使用CSS选择器 .

    Debug.Print html.querySelector("span[data-toggle=dropdown]").innerText
    

    使用选择器的CSS查询:

    css selector

    这适用于 span[data-toggle=dropdown] 的CSS选择器,它是具有 span 标签的元素,其属性为 data-toggle ,值为 dropdown . 我使用了 querySelector ,它只返回第一个匹配,因为你显示的HTML只有一个匹配的元素 .

    如果您有多个匹配元素,那么您将需要 querySelectorAll 方法,该方法返回 nodeList 匹配元素,然后使用适当的索引索引到基于0的 nodeList 中 .

    html.querySelectorAll("span[data-toggle=dropdown]").item(0).innerText
    

    您将遍历 nodeList.Length 以获取所有项目,例如

    Dim aNodeList As Object, i As Long, erow As Long, count As Long
    Set aNodeList  =  html.querySelectorAll("span[data-toggle=dropdown]")
    For i = 0 To aNodeList.Length -1
        erow = Sheets("Exec").Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
        Sheets("Exec").Cells(erow, 1)  =  aNodeList.item(i).innerText
        count = count + 1
    Next i
    

相关问题