赛普拉斯按部分元素名称查找元素

Cypress Finding element by partial element name

我想在 cypress 测试中获取这个元素,但只使用了没有 'undefined' 的字符串的一部分。我该怎么做?

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

使用 XPath 可以轻松完成。

  1. 安装cypress-xpath插件
  npm i -D cypress-xpath
  1. 然后包含在您项目的 cypress/support/index.js
  require('cypress-xpath')
  1. 安装后,您的 cy 对象将具有 xpath 命令
  cy.xpath("//*[starts-with(name(), 'line-chart-')]")

它将找到与 line-chart- 标签匹配的元素