Contains返回第一个文本仅包含给定字符串部分的元素。

huangapple go评论45阅读模式
英文:

Contains return the first element whose text has only a part of given string

问题

My question is about assertions on a table. I am trying to find an element with certain text, in this case the text is a date-time string. The problem occurs when using contains. If I write cy.contains('td', '04/01/2023 08:00').parent() and then cy.contains('td', '04/01/2023 16:00').parent(), when the DOM looks like this, the query for the first element works fine, but for the second one Cypress finds the element is not visible and then .parent() sends it to the first element. So contains returns the first element that has a part of the given string. Any ideas how this could be solved?

英文:

My question is about assertions on a table. I am trying to find an element with certain text, in this case the text is a date-time string.

The problem occurs when using contains.

Contains返回第一个文本仅包含给定字符串部分的元素。

If I write cy.contains('td', '04/01/2023 08:00').parent() and then cy.contains('td', '04/01/2023 16:00').parent(), when the DOM looks like this

Contains返回第一个文本仅包含给定字符串部分的元素。 &

Contains返回第一个文本仅包含给定字符串部分的元素。

T query for first element work just fine, but for the second one Cypress finds the element is not visible and then .parent() sends it to the first element.

So pretty much contains returns the first element that has a part of the given string.

Any ideas how this could be solved?

答案1

得分: 5

可以使用.filter()命令在所需文本上进行精确匹配。

cy.get('td').filter((index, td) => td.innerText === '04/01/2023 16:00')
  .parent()

或者直接使用父行

cy.get('tr').filter((index, tr) => tr.innerText === '04/01/2023 16:00')
英文:

It is possible to do an exact match on the text you want by using a .filter() command.

cy.get('td').filter((index, td) => td.innerText === '04/01/2023 16:00')
  .parent()

Or use the parent row directly

cy.get('tr').filter((index, tr) => tr.innerText === '04/01/2023 16:00')

huangapple
  • 本文由 发表于 2023年4月19日 21:30:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定