admin管理员组文章数量:1392002
I am having trouble selecting buttons on a page which has the same class name, but different data-ids in Cypress.
There are multiple courses with different ids, and I need to loop through each course and click on the Register
button. There are other buttons on the page (View Course), but I only want to click on the Register
button.
I tried the code below but no luck in Cypress:
cy.get(`.university [data-course-id=${id}] > button`).click()
cy.get(`.university [data-course-id=“1234”}] > button`).click()
Also tried this in Chrome Dev Tools to get a specific button. I need to get each Register button on the page.
$('.university [data-course-id=“1234”] > button')
But Cypress says it cannot find this element.
What is the issue? How would we select all the Register buttons and click each one?
Thanks for the help!
<div class="university">
<div class="course" data-course-id="1234">
<div class="courseTitle">
<span>Computer Science</span>
<button>View Course</button>
</div>
<button>Register</button>
</div>
<br>
<div class="course" data-course-id="bio3">
<div class="courseTitle">
<span>Biology</span>
<button>View Course</button>
</div>
<button>Register</button>
</div>
<br>
<div class="course" data-course-id="987b">
<div class="courseTitle">
<span>English</span>
<button>View Course</button>
</div>
<button>Register</button>
</div>
</div>
I am having trouble selecting buttons on a page which has the same class name, but different data-ids in Cypress.
There are multiple courses with different ids, and I need to loop through each course and click on the Register
button. There are other buttons on the page (View Course), but I only want to click on the Register
button.
I tried the code below but no luck in Cypress:
cy.get(`.university [data-course-id=${id}] > button`).click()
cy.get(`.university [data-course-id=“1234”}] > button`).click()
Also tried this in Chrome Dev Tools to get a specific button. I need to get each Register button on the page.
$('.university [data-course-id=“1234”] > button')
But Cypress says it cannot find this element.
What is the issue? How would we select all the Register buttons and click each one?
Thanks for the help!
<div class="university">
<div class="course" data-course-id="1234">
<div class="courseTitle">
<span>Computer Science</span>
<button>View Course</button>
</div>
<button>Register</button>
</div>
<br>
<div class="course" data-course-id="bio3">
<div class="courseTitle">
<span>Biology</span>
<button>View Course</button>
</div>
<button>Register</button>
</div>
<br>
<div class="course" data-course-id="987b">
<div class="courseTitle">
<span>English</span>
<button>View Course</button>
</div>
<button>Register</button>
</div>
</div>
cy.get(.appMovieContainer [data-movie-id=${id}] > button
).click()
3 Answers
Reset to default 3You can do something like this. This will iterate through all the .course
classes and inside that click on the button with text as Register one by one.
cy.get('.course').each(($ele) => {
cy.wrap($ele).find('button').contains('Register').click()
})
OR
cy.get('.course').each(($ele) => {
cy.wrap($ele).find('button').eq(1).click()
})
The problem with
cy.get(`.university [data-course-id=“1234”}] > button`).click()
is
- the rogue
}
, which is probably a typo - the quote marks around
1234
are not recognized by Cypress.
Try this out:
expect('“').not.to.eq('"') // true
console.log('“'.charCodeAt(0)) // 8220
console.log('"'.charCodeAt(0)) // 34
The correct version is
cy.get('.university [data-course-id="1234"] > button')
There's nothing wrong with .each()
, but another approach is to use :contains()
in the selector
cy.get('.university .course button:contains("Register")')
.click({multiple: true}) // "multiple" will click all matching buttons
You can try cy.get('[button="Register"]')
and then use each
method to perform click operation.
Cypress each
本文标签:
版权声明:本文标题:javascript - How to get all specific buttons and click on each one with a specific data-id in Cypress - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744668675a2618689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论