Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

65
Views
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:

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?

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

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)
  })
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs