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:
cy.get('[foo="bar"] a')
this should give me all the elements that have the parent (or grandparent foo, and is <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.
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?
I think the step .invoke('attr', 'href')
is just returning the first, see .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)
})