Home > OS >  cypress doesn't recognize the element I'm trying to find by placeholder
cypress doesn't recognize the element I'm trying to find by placeholder

Time:10-06

the element:
<input type="text" class="form-control" placeholder="What's this article about?"

How did I identify:
cy.get("input[placeholder=What's this article about?]").type("<3");

the error:
Syntax error, unrecognized expression: input[placeholder=What's this article about?]

CodePudding user response:

Try this:

cy.get("input[placeholder=\"What's this article about?\"]").type("<3");

or this with less escape characters:

cy.get('input[placeholder="What\'s this article about?"]').type("<3");

CodePudding user response:

Another way can be to use the [attribute*=value] selector. This selects every element whose attribute value contains the substring of the original string. So in your case it could be [placeholder*="article"] or [placeholder*="article about"], basically you can add any substring you want.

Word of caution - This should only be used when you know for sure that the occurrence of the sub string that your are searching for is once, that is your target element.

cy.get('input[placeholder*="article about"]').type('<3')

Or if you have multiple occurrences, then you have to use eq to reach that element.

  • Related