Cypress: Error: Syntax error, unrecognized expression – How to escape

I’m writing Cypress tests for our first hopefully-soon-to-be-purchased Shopware plugin and stumbled over:

Error: Syntax error, unrecognized expression: a[href=#/sw/product/create]

The problem? Cypress is using jQuery internally and therefore you need to escape the parameter for cy.get properly.

Thankfully K K Agrawal wrote a blogpost about how to do that! He added a method to do it but unfortunately no easy to read list of all the characters which needs to be escaped, so here is our list.

Escaping works with one backslash, like \.!

  • ;
  • &
  • ,
  • .
  • +
  • *
  • ~
  • :
  • !
  • ^
  • #
  • $
  • %
  • @
  • [
  • ]
  • (
  • )
  • =
  • >
  • |
# So my 
cy.get('a[href^="#/sw/product/create"]').click();
# needed to be
cy.get('a\[href\^\=\"\#\/sw\/product\/create\"\]').click();

P.S. I don’t know why, but my tests with Cypress.$.escapeSelector() didn’t work ?

Leave a Reply