【问题标题】:Ideal selector for Cypress automation Javascript赛普拉斯自动化 Javascript 的理想选择器
【发布时间】:2021-10-09 17:11:00
【问题描述】:

我有这个 HTML 代码

<li class="menu-level-1">

    <a href="/Public/app/#/calendar">
        <i class="site-menu-icon material-icons">date_range</i>
        <span>
            Calendar
        </span>

    </a>
</li>

我不知道我需要在 CYPRESS 中选择什么来自动按下日历按钮。我没有唯一的 css 或 id 类,因此我无法将其与其他菜单项隔离开来。我只有在这段代码中看到的内容。

【问题讨论】:

    标签: javascript css-selectors cypress


    【解决方案1】:

    我想你想点击元素&lt;a href="/Public/app/#/calendar"&gt;,因为它有href

    有很多方法可以定位它,使用哪种方法取决于页面上的独特之处

    cy.contains('a', 'Calendar').click()  // if the word Calendar only appears on this link
    
    cy.get('a[href="/Public/app/#/calendar"]').click()  // specifies by the link itself
    
    cy,get('i.site-menu-icon').parent().click()  // if the icon is the unique thing
    

    【讨论】:

      【解决方案2】:

      您可以使用自定义 xpath,例如 //*[text()="Calendar"]

      如果您在您的网页上找到了许多其他人,您可以提供一个类似//*[text()="Calendar"][1] 的索引,确保此处的索引始终以 1 开头,而不是 0。

      【讨论】:

      • 我有这个错误:“4000 毫秒后重试超时:预期找到元素://*[text()="Calendar"][1],但从未找到它。”
      • 你试过//*[text()="Calendar"]。我给了你一个提示,你需要传递一个正确匹配元素的索引。
      • 是的,我在使用 npm 安装并导入 xpath 后尝试了多种方法,但每次都出现相同的错误。我使用了以下语句:cy.xpath('code').click()
      • 你能把所有细节发给我,以便我在我的系统上运行它们吗?
      猜你喜欢
      • 2023-02-12
      • 2021-12-08
      • 2019-09-13
      • 2021-12-27
      • 1970-01-01
      • 2021-12-11
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多