Menu
Open source
Parse HTML
Examples parsing HTML content. Use the k6/html
module for HTML parsing.
Name | Type | Description |
---|---|---|
Selection | Class | A jQuery-like API for accessing HTML DOM elements. |
Element | Class | An HTML DOM element as returned by the Selection API. |
parseHTML(src) | function | Parse an HTML string and populate a Selection object. |
JavaScript
import { parseHTML } from 'k6/html';
import http from 'k6/http';
export default function () {
const res = http.get('https://k6.io');
const doc = parseHTML(res.body); // equivalent to res.html()
const pageTitle = doc.find('head title').text();
const langAttr = doc.find('html').attr('lang');
}
JavaScript
import { parseHTML } from 'k6/html';
import { sleep } from 'k6';
export default function () {
const content = `
<dl>
<dt id="term-1">Value term 1</dt>
<dt id="term-2">Value term 2</dt>
</dl>
`;
const sel = parseHTML(content).find('dl').children();
const el1 = sel.get(0);
const el2 = sel.get(1);
console.log(el1.nodeName());
console.log(el1.id());
console.log(el1.textContent());
console.log(el2.nodeName());
console.log(el2.id());
console.log(el2.textContent());
sleep(1);
}
Was this page helpful?
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
Video
Performance testing and observability in Grafana Cloud
In this webinar, learn how Grafana Cloud k6 offers you the best developer experience for performance testing.
User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.