0

标题不好,我知道,但很难用几句话来解释。

我在夜间值班时进行自动化,遇到了一些让我难过的事情,因为我习惯于在 JS 上编码只是为了自动化。通常,很容易验证元素是否存在。但为此,我有一个显示搜索结果的 html 元素,我需要验证结果是否仅包含元素

所以,在这个例子中,我需要确保divresult-item上的所有entity-results结果都只有hint-val-fragment match等于-1000000.0hint-prop等于的结果。cpuLimit:

<div class="results" style="">
   <!----> 
   <div>
      <div class="results-set-title">Results</div>
      <!----> 
      <div class="entity-results">
         <div class="result-item">
            <div class="result-item-label">
               <div class="result-item-name">Resources</div>
               <div class="result-item-source"><span>source:</span> <span>cz0</span></div>
            </div>
            <div class="result-item-hint">
               <div class="hint-prop">cpuLimit:</div>
               <div class="hint-val">
                  <div class="hint-val-fragment">
                  </div>
                  <div class="hint-val-fragment match">
                     -1000000.0
                  </div>
                  <div class="hint-val-fragment">
                  </div>
               </div>
            </div>
         </div>
         <div class="result-item">
            <div class="result-item-label">
               <div class="result-item-name">Resources</div>
               <div class="result-item-source"><span>source:</span> <span>cz0</span></div>
            </div>
            <div class="result-item-hint">
               <div class="hint-prop">cpuLimit:</div>
               <div class="hint-val">
                  <div class="hint-val-fragment">
                  </div>
                  <div class="hint-val-fragment match">
                     -1000000.0
                  </div>
                  <div class="hint-val-fragment">
                  </div>
               </div>
            </div>
         </div>
         <div class="result-item">
            <div class="result-item-label">
               <div class="result-item-name">Resources</div>
               <div class="result-item-source"><span>source:</span> <span>cz0</span></div>
            </div>
            <div class="result-item-hint">
               <div class="hint-prop">cpuLimit:</div>
               <div class="hint-val">
                  <div class="hint-val-fragment">
                  </div>
                  <div class="hint-val-fragment match">
                     -1000000.0
                  </div>
                  <div class="hint-val-fragment">
                  </div>
               </div>
            </div>
         </div>
         <div class="result-item">
            <div class="result-item-label">
               <div class="result-item-name">Resources</div>
               <div class="result-item-source"><span>source:</span> <span>cz0</span></div>
            </div>
            <div class="result-item-hint">
               <div class="hint-prop">cpuLimit:</div>
               <div class="hint-val">
                  <div class="hint-val-fragment">
                  </div>
                  <div class="hint-val-fragment match">
                     -1000000.0
                  </div>
                  <div class="hint-val-fragment">
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>
</div>
4

1 回答 1

0

下面我定义了 isValid,它应用于传递给 allValid 的数组中的每个元素。此功能可以在 DOM 更新后应用,您需要重新检查元素:

const isValid = elem => {
    const val = elem.getElementsByClassName('hint-val-fragment match')[0].innerHTML.trim()
    const hint = elem.getElementsByClassName('hint-prop')[0].innerHTML.trim()
    return (
        val === '-1000000.0'
        && hint === 'cpuLimit:'
    )
}
const allValid = arr => arr.every(isValid)


const parent = document.getElementsByClassName('entity-results')[0]
const children = Array.from(parent.children)


const allAreValid = allValid(children)
于 2019-09-15T22:53:42.643 回答