MichalG
MichalG

Reputation: 381

Cypress Finding element by partial element name

I would like to get this element in cypress tests but using only part of string without 'undefined'. How can I do that?

<line-chart-undefined tabindex="0"></line-chart-undefined>

Upvotes: 2

Views: 780

Answers (2)

Poodle
Poodle

Reputation: 202

Since cypress-xpath is now deprecated, this is how you would do it with CSS selectors

cy.get('parent-element')
  .children()
  .filter((index, element) => element.tagName.startsWith('line-chart'))

Upvotes: 4

Yevhen Laichenkov
Yevhen Laichenkov

Reputation: 8690

It can be easily done with XPath.

  1. Install cypress-xpath plugin
  npm i -D cypress-xpath
  1. Then include in your project's cypress/support/index.js
  require('cypress-xpath')
  1. After installation your cy object will have xpath command
  cy.xpath("//*[starts-with(name(), 'line-chart-')]")

It'll find the element that matches the line-chart- tag

Upvotes: 1

Related Questions