赛普拉斯-如何使用文本字段中的值获取元素

时间:2020-07-20 22:41:30

标签: automation cypress

我的应用程序中有几个text_area_field,只能通过使用其中的值来区分。如何使用值获取特定元素?

例如:

<div data-component="options">
    <input data-component="text_area">Value 1</input>  
    <input data-component="text_area">Value 2</input>  
    <input data-component="text_area">Value 3</input>  
    <input data-component="text_area">Value 4</input> 

我尝试过 cy.get('[data-component="options"] [data-component="text_area"]').contains('Value 3').clear().type('Edited 3')。 但是它给出了错误: 重试超时:预计会在元素中找到内容:“值3”:但从未尝试

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

检查div是否允许键入。如果是,请尝试以下代码,看看是否有效:

cy.get('[data-component="options"] [data-component="text_area"]').each(($div, i) => {
            const valueText = Cypress.$($div).text();
            console.log(valueText);
            if(valueText === "Value 3") {
              cy.wrap($div).clear().type("Edited 3");
            }
        }) 

您也可以尝试使用eq(),但是如果选择器中的更改带有更多选项,则可能会失败。

cy.get('[data-component="options"] > div').eq(2).then(($div)=>{
     cy.wrap($div).contains("Value 3").clear().type("Edited 3");
 })

您也可以尝试以下方法:

cy.get('[data-component="options"] > div').contains("Value 3").clear().type("Edited 3");

答案 1 :(得分:0)

我认为这可以。请尝试以下代码:

cy.get('[data-component="options"] > div').invoke('show').eq(2).then(($div)=>{
 cy.wrap($div).contains("Value 3").clear().type("Edited 3");
 })