Reputation: 181
Cypress: I want to find an element from the list of an element based on text and want to click on that
cy.get('partie-room-list-item > div > div.content-block > span.partie-title').each(($elm , i) => {
cy.get($elm).invoke('text').then((text) => {
if (text === 'textinelement') {
expect(text.trim()).equal('textinelement').click();
cy.log(text)
}
})
});
cypress find element and click https://www.screencast.com/t/p0rL6qexD5
Upvotes: 3
Views: 11794
Reputation: 9706
You want to use cy.contains()
(see documentation).
For example:
cy.contains('textinelement').click();
You can also add specificity so it only looks for text within a specified selector:
cy.contains('.partie-title', 'textinelement').click();
Upvotes: 0
Reputation: 11
it("This test is for selecting a single element based on text",()=>{
cy.visit('http://automationpractice.com/index.php')
cy.get("[title='Women']").each(($element, index)=>{
console.log("Object is "+ $element.text())
if($element.text()=='Women'){
cy.get($element).click()
}
})
})
or
it("This test is for selecting a single element based on text",()=>{
cy.visit('http://automationpractice.com/index.php')
cy.get("[title='Women']").contains('Women').click()
})
Upvotes: 1
Reputation: 13817
I am just guessing here. If you post your HTML I might be able to help you
cy.get('.partie-title').contains('textinelement').click()
looking at your image it would be like this
cy.get('.partie-title').contains('cypresssendfollowRequest').click()
Upvotes: 2
Reputation: 11951
Could you try below code and let me know the outcome. You could used .text()
to get the text from the span element.
cy.get('partie-room-list-item > div > div > .partie-title').each(ele => {
const eleText = "cypresssendfollowRequest";
if (ele.text() === eleText) {
ele.click();
}
});
Upvotes: 3