No results for

Powered byAlgolia

Parse HTML

Examples parsing HTML content. Use the k6/html module for HTML parsing.

NameTypeDescription
SelectionClassA jQuery-like API for accessing HTML DOM elements.
ElementClassAn HTML DOM element as returned by the Selection API.
parseHTML(src)functionParse an HTML string and populate a Selection object.
Select.find
1import { parseHTML } from 'k6/html';
2import http from 'k6/http';
3
4export default function () {
5 const res = http.get('https://k6.io');
6 const doc = parseHTML(res.body); // equivalent to res.html()
7 const pageTitle = doc.find('head title').text();
8 const langAttr = doc.find('html').attr('lang');
9}
Element
1import { parseHTML } from 'k6/html';
2import { sleep } from 'k6';
3
4export default function () {
5 const content = `
6<dl>
7 <dt id="term-1">Value term 1</dt>
8 <dt id="term-2">Value term 2</dt>
9</dl>
10 `;
11 const sel = parseHTML(content).find('dl').children();
12
13 const el1 = sel.get(0);
14 const el2 = sel.get(1);
15
16 console.log(el1.nodeName());
17 console.log(el1.id());
18 console.log(el1.textContent());
19
20 console.log(el2.nodeName());
21 console.log(el2.id());
22 console.log(el2.textContent());
23
24 sleep(1);
25}