簡體   English   中英

如何從 cypress 中括號中的文本元素中提取文本,即測試(5)

[英]How to extract text from element having text in brackets in cypress i.e. test(5)

有人可以幫助從元素中提取文本嗎? 假設我們在頁面中顯示了具有文本“Test(10)”的元素。 我們如何分別獲得文本作為 o/p 測試和 10。

提前致謝!

您可以將正則表達式與名稱捕獲組一起使用。

// example for 'Test(10)'
cy.get('.element-selector')
  .invoke('text')
  .then(elText => {
    const matcher = (?<text>test)\((?<number>\d+)\)
    // extracts 'Test'
    const text = elText.match(matcher)?.groups?.text
    // extracts '10'
    const num = elText.match(matcher)?.groups?.num
  })

暫無
暫無

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

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