英文:
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
.
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
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论