Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Get line separated text from element with <br> tags from cypress

I'm trying to get contact information through cypress and compare it with database. So I need to split the details and get Title, First Name, Last Name, Country... etc.

But the text contain in a single div and it has been separated using <br> tag.

Then I have tried to get the text as follows:

cy.xpath('element xpath').invoke('text').then((AddressLineText) => {
        cy.log(AddressLineText);
});

Flowing is the invoked text and it has been connected the line texts without even a space. Due to that I'm unable to split by space or /n.

Cypress Log for invoke text

Bellow is the HTML structure:

<div class="address">
    Mr. Test User
    <br>
    Testing Services Ltd
    <br>
    Britannia House
    <br>
    Rushmills
    <br>
    NN4 7YB, Northampton
    <br>
    Northamptonshire
    <br>
    United Kingdom
</div>

How to isolate each lines according to this scenario.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The <br> tags become newlines after text extract

cy.get('.address')
  .invoke('text')
  .then(text => text.split('\n'))                // split by newline
  .then(texts => texts.map(text => text.trim()))  // trim whitespace
  .then(texts => texts.filter(text => text))     // remove empty
  .then(texts => {
    console.log(texts)
  })
})

prints

["Mr. Test User", "Testing Services Ltd", "Britannia House", "Rushmills", "NN4 7YB, Northampton", "Northamptonshire", "United Kingdom"]

Node.TEXT_NODE

Extracting the text nodes of the div is better if the content is more complicated.

  • use .childNodes to get all <br> and texts within address <div>
  • filter children by node type to remove <br>
  • get node values and trim spaces and newlines
cy.get('.address')
  .then($el => $el[0].childNodes)  
  .then(children => [...children].filter(child => child.nodeType === Node.TEXT_NODE))
  .then(textNodes => textNodes.map(textNode => textNode.nodeValue.trim()))
  .then(texts => {
    console.log(texts)
  })

prints

["Mr. Test User", "Testing Services Ltd", "Britannia House", "Rushmills", "NN4 7YB, Northampton", "Northamptonshire", "United Kingdom"]
about 4 years ago · Juan Pablo Isaza Report

0

You can use each() to get all your individual texts printed. One line for each text in the test runner logs.

cy.xpath('element xpath').each(($ele) => {
   cy.log($ele.text());
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!