使用VBA获取没有类名的跨度值

时间:2018-07-26 04:25:34

标签: excel vba excel-vba

我被困住了,

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代码中找到,我希望使用我的Excel VBA代码获得该字母(高),请帮帮我!!

请在我的HTML代码中找到,我希望使用我的Excel VBA代码获得该字母(高),请帮帮我!!

2 个答案:

答案 0 :(得分:0)

如果您的意思是“ ”,则可以使用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

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

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

答案 1 :(得分:0)

使用传统方式。

在循环内更改 ===>
html.getElementsByTagName("span")(count).innerText < br /> 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