【问题标题】:How to write dynamic xpath in cypress如何在 cypress 中编写动态 xpath
【发布时间】:2022-11-26 06:06:29
【问题描述】:

说我有元素

 <div id="Test1">
      <p text="One"> One </p>
 </div>

 <div id="Test2">
      <p text="One">  One </p>
 </div>

 <div id="Test3"> 
      <p text="One"> One </p>
 </div>

 <div id="Test4"> 
      <p text="One"> One </p>
 </div>

现在我想根据 div id 动态定位文本元素。就像是

getText(divID){

cy.get("divID").find("vasErrorMessage").; }

如何为此在柏树中编写代码。在这里我将动态发送 divID 并且 divID 可以是 Test2 或 Test1

【问题讨论】:

  • 请向我们展示您的代码试用版。您遇到过哪些具体问题、错误?

标签: css xpath cypress


【解决方案1】:

当使用 Id 作为 CSS 选择器时,它需要以散列作为前缀。

getElementById(id) {
    const selector = "#" + id;
    return cy.get(selector);
}

不需要 xpath,这是开箱即用的 Cypress 不支持的。

【讨论】:

    【解决方案2】:

    您可以使用.contains() 来获取正确的元素。

    // gets third id=Test3 element with 'One' text
    cy.contains('#Test3', 'One')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 2015-10-29
      • 2023-03-14
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 2012-12-30
      相关资源
      最近更新 更多