【问题标题】:Typing into a html box using cy.get with cypress/javascript not finding the id's使用 cy.get 和 cypress/javascript 输入 html 框时找不到 id
【发布时间】:2018-05-16 16:06:35
【问题描述】:

我是 cypress/html 的新手,我正在尝试在以下地址框中输入地址

<input type="text" title="Street Address 1" name="billing[street][]" id="billing:street1" value="" class="input-text " autocomplete="address-line1">

所以我想做的是在这个框中输入一些东西,然后我就写了

cy.get('#billing:street1').type('1322 the gulag archipelago')

但是我在 cypress 中收到以下错误:

预计会找到“#billing:street1”,但从未找到。

所以我的问题是为什么这不起作用,因为我以前做过这个 .get by id 并且它已经起作用了。

编辑: 是不是我需要使用'billing:street1'或类似的东西来指定 : 的含义。我专门试过这个,它没有用。

提前谢谢你。

【问题讨论】:

    标签: javascript html cypress


    【解决方案1】:

    有多种方法可以做到这一点。

    使用元素标签本身:

    cy.get('input').type('1322 the gulag archipelago');
    

    使用属性标签之一:

    cy.get('[title="Street Address 1"'].type('1322 the gulag archipelago');
    
    cy.get('[id="billing:street1"]).type('1322 the gulag archipelago');
    
    cy.get('[class="input-text "]').type('1322 the gulag archipelago');
    

    从表单元素中:

    cy.get('[sample-form-id="ExampleAddressForm"]').within(() => {
        cy.get('input').type('1322 the gulag archipelago');
    });
    

    如果这是页面上的第三个输入:

    cy.get('input').then((inputs) => {
        cy.get(inputs[3]).type('1322 the gulag archipelago');
    });
    

    【讨论】:

      【解决方案2】:

      这就是我如何让它工作的我可以使用类似 jQuery 的命令来查询标题这实际上更强大,因为 id 可以更改但很可能标题不会因此命令

      cy.get('[title|="Street Address 1"]').type('1322 the gulag archipelago')
      

      工作得很好,因为您可以在 cypress 中执行几乎所有的 jQuery 操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-06
        • 1970-01-01
        • 1970-01-01
        • 2019-04-04
        • 2014-01-03
        • 1970-01-01
        • 1970-01-01
        • 2020-08-05
        相关资源
        最近更新 更多