简体   繁体   English

如何获取元素所有子元素的所有链接

[英]How to get all the links of all children of an element

I have a element that I am able to locate:我有一个可以找到的元素:

cy.get('[foo="bar"]')

Now I'm trying to locate all the a elements within said element:现在我试图找到所述元素中的所有a元素:

cy.get('[foo="bar"] a') this should give me all the elements that have the parent (or grandparent foo, and is <a> . cy.get('[foo="bar"] a')这应该给我所有具有父(或祖父 foo,并且是<a>的元素。

However, this only seems to select one element (that is parent foo and a), not all the elements, since I'm only getting one href saved.但是,这似乎只选择了一个元素(即父 foo 和 a),而不是所有元素,因为我只保存了一个 href。

This is what I use to save the results:这是我用来保存结果的:

cy.get('[foo="bar"]').invoke('attr', 'href')
            .then((hrefs) => {
            cy.writeFile('./cypress/downloads/results.txt', hrefs)
        });     

How do I select all the children, that are <a> , of [foo="bar"] and then how do I extract all of their href attributes?如何选择[foo="bar"]的所有子元素,即<a> ,然后如何提取它们的所有 href 属性?

I think the step .invoke('attr', 'href') is just returning the first, see .attr()我认为步骤.invoke('attr', 'href')只是返回第一个,请参阅.attr()

Get the value of an attribute for the first element in the set of matched elements获取匹配元素集中第一个元素的属性值

So, maybe所以,也许

cy.get('[foo="bar"] a')
  .then($els => {
    const hrefs = [...$els].map(el => el.getAttribute('href'))
    cy.writeFile('./cypress/downloads/results.txt', hrefs)
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM