[英]How to extract text from element having text in brackets in cypress i.e. test(5)
Can someone pls help in extracting text from element.有人可以帮助从元素中提取文本吗? Let us say we have element shown in page having text 'Test(10).
假设我们在页面中显示了具有文本“Test(10)”的元素。 How we can get text as o/p Test and 10 separately.
我们如何分别获得文本作为 o/p 测试和 10。
Thanks in advance!提前致谢!
You can use regex with name capture group.您可以将正则表达式与名称捕获组一起使用。
// 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.