Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write cypress locator for this dynamic button specific to the span element "Agile Fundamentals"

I am new to cypress automation. I tried to use the following locator to click on the specific dynamic button.

cy.contains(‘span’, ‘Agile Fundamentals’).next().find(‘button’).click()

but it did not work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

.next() won’t work in this case because the next element would be the text label with the date.

Instead, we can to use .parent() and .siblings() to move over to the other div and grab that button.

cy.contains('span', 'Agile Fundamentals')
  .parent() // moves us to the div around the span
  .siblings('div') // moves us to the sibling div
  // could use `.next()` above instead
  .find('button') // moves us to the button
  // If you want to save this for ease of use, you can assign it an alias
  .as('dynamicButton')
  // and reference it like...
cy.get('@dynamicButton')...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading