Extracting href from response

can anyone help me in getting the below id captured in K6 script:

<div class="yCmsComponent js_nav__link">
<a href="/pe/ofertas/c/dest-06" title="Tester">Tester</a></div></div>
</li>

We have multiple values with the same boundary, I have to capture the value inside ahref and title , how we can do that and keep it in an array to use further.

Hi, welcome to the forum :slight_smile:

Take a look at the parseHTML() function.

You could use it like so:

import { parseHTML } from 'k6/html';

export default function () {
  const content = `
<div class="yCmsComponent js_nav__link">
<a href="/pe/ofertas/c/dest-06" title="Tester">Tester</a></div></div>
</li>
  `;
  // Make sure the selector is precise enough.
  const sel = parseHTML(content).find('div > a'),
        el = sel.get(0),
        attrs = el.attributes();

  console.log(attrs.href.value);
  console.log(attrs.title.value);
}

Which when run outputs:

INFO[0000] /pe/ofertas/c/dest-06                         source=console
INFO[0000] Tester                                        source=console

Also, if you’re getting this HTML from a response, you can use Response.html().

1 Like