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

Go back to parent element – CSS selector in Cypress

Can someone please assist in following case:

I have to check in if statement does element exist. Since there is no possibility to use Xpath in find command (Throws Syntax error when run it with Xpath).

My code looks:

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

cy.xpath(list)
    .eq(index)
    .then(($el1) => {
      cy.get('body').then((body) => {
        if (
          body.find(
            currentBase +
              currentTitle[index] +
              currentExtension
          ).length > 0
        ) {
     ...

Where currentBase is Xpath before text, currentText – element with text and currentExtension is concatentation to get element below that text element.
I do not want to use those classes since they are dynamic ones (also, can not be changed with some unique attribute in near future)

And DOM looks:

enter image description here

Namely, easily is found marked img element, but with following CSS, it does not work

#structures img[src*="/static/media/image"].$('..').$('..').$('..') div:nth-child(2)

What I want is, to find div below element with text Element One

What I am doing wrong?
Or is there any other way in Cypress to use together if statement and to pass that step if element is not found?
Thank you in advance

>Solution :

You can do something like this. This will get you the div element just below Element One and which is also the parent element for the img

cy.get('img[src*="/static/media/image"]').parent('div')
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