【问题标题】:How can I use Cypress to select an <option> in a specific HTML <select> field?如何使用赛普拉斯在特定的 HTML <select> 字段中选择 <option>?
【发布时间】:2019-05-18 17:09:08
【问题描述】:

我想从下面的 HTML 代码中选择选项。 timeentry_lov1 是选择名称,它是 Page 中具有相同名称的第二个元素。我还检查了页面中没有框架。 但是 Cypress 中有没有像 selenium 这样的框架概念?如果没有,那么如何使用 Select 属性?

HTML:

]1

我尝试过的以下 Select 语句:

cy.get('timeentry_lov1',{timeout : 60000}).eq(1).select('NB-860').debug();
cy.get('timeentry_lov1').eq(1).select('NB-860-Test Automation');
cy.select('timeentry_lov1',{timeout : 7000}).eq(1).should('have.value','NB-860');
cy.find('timeentry_lov1').get('select').select('NB-860');

【问题讨论】:

    标签: javascript testing automation cypress


    【解决方案1】:

    要在页面中查找名称等于“timeentry_lov1”的所有&lt;select&gt; 元素,请使用CSS selector 过滤name 属性,如下所示:

    cy.get('select[name="timeentry_lov1"]')
    

    要仅选择 second &lt;select&gt; 标记,您可以使用 :nth-of-type(2) 仅选择找到的第二个元素:

    cy.get('select[name="timeentry_lov1"]:nth-of-type(2)')
    

    现在,要实际选择元素,只需将cy.select() 与您要选择的&lt;option&gt;value 一起使用:

    cy.get('select[name="timeentry_lov1"]:nth-of-type(2)')
    .select('NB-860')
    

    这应该可以完成您正在尝试做的事情。


    提示:您可以使用 Cypress 的 Selector Playground 来帮助您找到在测试中使用的优秀 CSS 选择器。查看文档以获取有关其工作原理的视频。

    【讨论】:

    • 感谢您提供 Selector Playgound 链接。使用该页面构建工作选择器
    猜你喜欢
    • 2018-11-15
    • 1970-01-01
    • 2019-09-13
    • 2019-12-18
    • 2020-10-20
    • 1970-01-01
    • 2021-12-27
    • 2021-09-21
    • 2023-03-28
    相关资源
    最近更新 更多