Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cypress 如何循环遍历属性值并检查它是否包含字符串_Cypress - Fatal编程技术网

Cypress 如何循环遍历属性值并检查它是否包含字符串

Cypress 如何循环遍历属性值并检查它是否包含字符串,cypress,Cypress,我有以下html代码,并希望记录valueOfInterest: <div id="123"> <span some-tag="valueOfInterest1"> <span some-tag="valueOfInterest2"> <span some-tag="valueOfInterest3"></div> 对不起,我是柏树的初学者。。。但有什么不

我有以下html代码,并希望记录valueOfInterest:

<div id="123">
<span some-tag="valueOfInterest1">
<span some-tag="valueOfInterest2">
<span some-tag="valueOfInterest3"></div>
对不起,我是柏树的初学者。。。但有什么不对呢? 上面的错误消息是:无法读取未定义的属性“getAttribute”。
但这只是我尝试过的众多解决方案之一。提前感谢。

我认为问题在于您不需要使用索引获取元素。相反,您传递的
$el
已经是您想要的元素

cy.get('div[id="123"] span').each(($el) => {
    const atri = $el.getAttribute('some-tag');
    cy.log(atri);
});

要访问属性值,可以使用


无需使用索引i,因为each循环为您完成以下任务:

cy.get("#123").children().each((el)=>{
    cy.log(el.getAttribute('some-tag'))
})

在本例中,它说:$el.getAttribute不是一个函数。是的,我错了。看看@rosen的回答在本例中,eslint抱怨“没有过载匹配此呼叫…”。但是,代码是有效的。您的ES lint可能没有配置为cypress。您应该避免只使用代码。提供基本原理的答案更有可能有用
cy.get('div[id="123"] span').each(($el, i) => {
     cy.get($el)
        .should('have.attr', 'some-tag')
        .then(attrValue => cy.log(attrValue))
});
cy.get("#123").children().each((el)=>{
    cy.log(el.getAttribute('some-tag'))
})