Skip to main content
The k6/html module provides HTML parsing and DOM manipulation using jQuery-like API.

Functions

parseHTML(src)

Parses HTML string and returns a Selection object.
string
HTML content to parse
Selection
Selection object for querying the DOM

Selection Object

The Selection object provides jQuery-like methods for querying and manipulating HTML.

Traversal Methods

find(selector)

Finds descendants matching the selector.

filter(selector)

Filters current selection.

not(selector)

Removes elements matching selector from the selection.

first()

Returns first element in selection.

last()

Returns last element in selection.

eq(index)

Returns element at the specified index.

slice(start, [end])

Reduces selection to a subset.

next([selector])

Returns next sibling.

nextAll([selector])

Returns all following siblings.

prev([selector])

Returns previous sibling.

prevAll([selector])

Returns all preceding siblings.

parent([selector])

Returns parent element.

parents([selector])

Returns all ancestors.

children([selector])

Returns direct children.

siblings([selector])

Returns all siblings.

closest(selector)

Finds closest ancestor matching selector.

Content Methods

text()

Gets text content of all matched elements.

html()

Gets HTML content of first matched element.

attr(name, [default])

Gets attribute value.

val()

Gets value of form elements.

data([key])

Gets data attributes.

Iteration Methods

each(fn)

Iterates over selection.

map(fn)

Maps selection to array.

Utility Methods

size()

Returns number of elements in selection.

get([index])

Gets element(s) as array.

toArray()

Converts selection to array.

Examples

Form Data Extraction

Scraping Data

Validate Page Structure

Extract Meta Tags