<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>k6/html on Grafana Labs</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-html/</link><description>Recent content in k6/html on Grafana Labs</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/docs/k6/v2.3.x/javascript-api/k6-html/index.xml" rel="self" type="application/rss+xml"/><item><title>parseHTML( src )</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-html/parsehtml/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-html/parsehtml/</guid><content><![CDATA[&lt;h1 id=&#34;parsehtml-src-&#34;&gt;parseHTML( src )&lt;/h1&gt;
&lt;p&gt;Parse an HTML string and populate a 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/&#34;&gt;Selection&lt;/a&gt; object.&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Parameter&lt;/th&gt;
              &lt;th&gt;Type&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;src&lt;/td&gt;
              &lt;td&gt;string&lt;/td&gt;
              &lt;td&gt;HTML source.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h3 id=&#34;returns&#34;&gt;Returns&lt;/h3&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Type&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/&#34;&gt;Selection&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;A Selection object.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h3 id=&#34;example&#34;&gt;Example&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { parseHTML } from &amp;#39;k6/html&amp;#39;;
import http from &amp;#39;k6/http&amp;#39;;

export default function () {
  const res = http.get(&amp;#39;https://k6.io&amp;#39;);
  const doc = parseHTML(res.body); // equivalent to res.html()
  const pageTitle = doc.find(&amp;#39;head title&amp;#39;).text();
  const langAttr = doc.find(&amp;#39;html&amp;#39;).attr(&amp;#39;lang&amp;#39;);
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="parsehtml-src-">parseHTML( src )&lt;/h1>
&lt;p>Parse an HTML string and populate a
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-html/selection/">Selection&lt;/a> object.&lt;/p>
&lt;section class="expand-table-wrapper">&lt;div class="button-div">
&lt;button class="expand-table-btn">Expand table&lt;/button>
&lt;/div>&lt;div class="responsive-table-wrapper">
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Parameter&lt;/th>
&lt;th>Type&lt;/th>
&lt;th>Description&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>src&lt;/td>
&lt;td>string&lt;/td>
&lt;td>HTML source.&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;/div>
&lt;/section>&lt;h3 id="returns">Returns&lt;/h3>
&lt;section class="expand-table-wrapper">&lt;div class="button-div">
&lt;button class="expand-table-btn">Expand table&lt;/button>
&lt;/div>&lt;div class="responsive-table-wrapper">
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Type&lt;/th>
&lt;th>Description&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-html/selection/">Selection&lt;/a>&lt;/td>
&lt;td>A Selection object.&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;/div>
&lt;/section>&lt;h3 id="example">Example&lt;/h3>
&lt;div class="code-snippet ">&lt;div class="lang-toolbar">
&lt;span class="lang-toolbar__item lang-toolbar__item-active">JavaScript&lt;/span>
&lt;span class="code-clipboard">
&lt;button x-data="app_code_snippet()" x-init="init()" @click="copy()">
&lt;img class="code-clipboard__icon" src="/media/images/icons/icon-copy-small-2.svg" alt="Copy code to clipboard" width="14" height="13">
&lt;span>Copy&lt;/span>
&lt;/button>
&lt;/span>
&lt;div class="lang-toolbar__border">&lt;/div>
&lt;/div>&lt;div class="code-snippet ">
&lt;pre data-expanded="false">&lt;code class="language-javascript">import { parseHTML } from &amp;#39;k6/html&amp;#39;;
import http from &amp;#39;k6/http&amp;#39;;
export default function () {
const res = http.get(&amp;#39;https://k6.io&amp;#39;);
const doc = parseHTML(res.body); // equivalent to res.html()
const pageTitle = doc.find(&amp;#39;head title&amp;#39;).text();
const langAttr = doc.find(&amp;#39;html&amp;#39;).attr(&amp;#39;lang&amp;#39;);
}&lt;/code>&lt;/pre>
&lt;/div>
&lt;/div></description></item><item><title>Element</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-html/element/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-html/element/</guid><content><![CDATA[&lt;h1 id=&#34;element&#34;&gt;Element&lt;/h1&gt;
&lt;p&gt;Represents a DOM element matched by a 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/&#34;&gt;Selection&lt;/a&gt;,
and provides an API to inspect the element content.&lt;/p&gt;
&lt;p&gt;Use 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-get/&#34;&gt;Selection.get(index)&lt;/a&gt; to return an Element object.&lt;/p&gt;
&lt;p&gt;The Element object provides a similar API to the &lt;a href=&#34;https://developer.mozilla.org/en-US/Web/API/Element&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;DOM Element API&lt;/a&gt; to retrieve element information.&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Method&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/element-selection/&#34;&gt;selection&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;The selection matching the element.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;nodeName&lt;/td&gt;
              &lt;td&gt;The name of the element.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;nodeType&lt;/td&gt;
              &lt;td&gt;The type of the element.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;nodeValue&lt;/td&gt;
              &lt;td&gt;The element value.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;id&lt;/td&gt;
              &lt;td&gt;The id of the element.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;innerHTML&lt;/td&gt;
              &lt;td&gt;Is a DOMString representing the markup of the element&amp;rsquo;s content.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;textContent&lt;/td&gt;
              &lt;td&gt;The element content.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;ownerDocument&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;attributes&lt;/td&gt;
              &lt;td&gt;An array of attributes.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;firstChild&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;lastChild&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;childElementCount&lt;/td&gt;
              &lt;td&gt;The number of children elements.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;firstElementChild&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;lastElementChild&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;previousSibling&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;nextSibling&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;previousElementSibling&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;nextElementSibling&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;parentElement&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;parentNode&lt;/td&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;childNodes&lt;/td&gt;
              &lt;td&gt;Array of 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;children&lt;/td&gt;
              &lt;td&gt;Array of 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;classList&lt;/td&gt;
              &lt;td&gt;An array of class names.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;className&lt;/td&gt;
              &lt;td&gt;The class name string&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;lang&lt;/td&gt;
              &lt;td&gt;The value of the lang attribute.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;toString&lt;/td&gt;
              &lt;td&gt;The element string representation.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;hasAttribute&lt;/td&gt;
              &lt;td&gt;Boolean&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;getAttribute&lt;/td&gt;
              &lt;td&gt;getAttributeNode&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;hasAttributes&lt;/td&gt;
              &lt;td&gt;Boolean&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;hasChildNodes&lt;/td&gt;
              &lt;td&gt;Boolean&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;isSameNode&lt;/td&gt;
              &lt;td&gt;Boolean&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;isEqualNode&lt;/td&gt;
              &lt;td&gt;Boolean&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;getElementsByClassName&lt;/td&gt;
              &lt;td&gt;Return an array of 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;getElementsByTagName&lt;/td&gt;
              &lt;td&gt;Return an array of 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;querySelector&lt;/td&gt;
              &lt;td&gt;Returns the first 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt; which matches the specified selector string relative to the element&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;querySelectorAll&lt;/td&gt;
              &lt;td&gt;Returns all the 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element&lt;/a&gt; which matches the specified selector string relative to the element&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;contains&lt;/td&gt;
              &lt;td&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;matches&lt;/td&gt;
              &lt;td&gt;Returns a Boolean indicating whether or not the element would be selected by the specified selector string&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;namespaceURI&lt;/td&gt;
              &lt;td&gt;The namespace URI of the element.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;isDefaultNamespace&lt;/td&gt;
              &lt;td&gt;Returns a Boolean indicating whether the element has the default namespace.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;Additionally, Element can provide more methods depending on the Element type.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;AnchorElement&lt;/strong&gt;: hash, host, hostname, port, username, password, origin, pathname, protocol, relist, search, text.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ButtonElement&lt;/strong&gt;: form, formAction, formEnctype, formMethod, formNoValidate, formTarget, labels, name, value.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;CanvasElement&lt;/strong&gt;: width, height&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;DataListElement&lt;/strong&gt;: options&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;FieldSetElement&lt;/strong&gt;: elements, type, form&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;FormElement&lt;/strong&gt;: elements, length, method&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;InputElement&lt;/strong&gt;: form&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;LabelElement&lt;/strong&gt;: control, form&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;LegendElement&lt;/strong&gt;: form&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;LinkElement&lt;/strong&gt;: relList&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;MapElement&lt;/strong&gt;: areas, images&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ObjectElement&lt;/strong&gt;: form&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OptionElement&lt;/strong&gt;: disabled, form, index, label, text, value&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OutputElement&lt;/strong&gt;: value, labels&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ProgressElement&lt;/strong&gt;: max, value, position&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ScriptElement&lt;/strong&gt;: text&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;SelectElement&lt;/strong&gt;: form, length, options, selectedOptions, selectedIndex, value&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;StyleElement&lt;/strong&gt;: text&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;TableElement&lt;/strong&gt;: caption, thead, tbody, tfoot, rows&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;TableCellElement&lt;/strong&gt;: cellIndex, colSpan, rowSpan, headers&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;TableRowElement&lt;/strong&gt;: cells, colSpan, sectionRowIndex, rowIndex&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;VideoElement&lt;/strong&gt;: textTracks&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;TitleElement&lt;/strong&gt;: text&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;example&#34;&gt;Example&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { parseHTML } from &amp;#39;k6/html&amp;#39;;
import { sleep } from &amp;#39;k6&amp;#39;;

export default function () {
  const content = `
&amp;lt;dl&amp;gt;
  &amp;lt;dt id=&amp;#34;term-1&amp;#34;&amp;gt;Value term 1&amp;lt;/dt&amp;gt;
  &amp;lt;dt id=&amp;#34;term-2&amp;#34;&amp;gt;Value term 2&amp;lt;/dt&amp;gt;
&amp;lt;/dl&amp;gt;
  `;
  const sel = parseHTML(content).find(&amp;#39;dl&amp;#39;).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);
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { parseHTML } from &amp;#39;k6/html&amp;#39;;
import { sleep } from &amp;#39;k6&amp;#39;;

export default function () {
  const content = `
\t&amp;lt;a href=&amp;#34;http://username:password@example.com:80&amp;#34; rel=&amp;#34;prev next&amp;#34; target=&amp;#34;_self&amp;#34; type=&amp;#34;rare&amp;#34; accesskey=&amp;#34;q&amp;#34; hreflang=&amp;#34;en-US&amp;#34; media=&amp;#34;print&amp;#34;&amp;gt;6&amp;lt;/a&amp;gt; &amp;lt;!-- trufflehog:ignore --&amp;gt;
  `;
  const el = parseHTML(content).find(&amp;#39;a&amp;#39;).get(0);

  console.log(el.nodeName());
  console.log(el.innerHTML());
  console.log(el.host());
  console.log(el.hostname());
  console.log(el.protocol());

  sleep(1);
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="element">Element&lt;/h1>
&lt;p>Represents a DOM element matched by a
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-html/selection/">Selection&lt;/a>,
and provides an API to inspect the element content.&lt;/p>
&lt;p>Use
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-get/">Selection.get(index)&lt;/a> to return an Element object.&lt;/p>
&lt;p>The Element object provides a similar API to the &lt;a href="https://developer.mozilla.org/en-US/Web/API/Element" target="_blank" rel="noopener noreferrer">DOM Element API&lt;/a> to retrieve element information.&lt;/p></description></item><item><title>Selection</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-html/selection/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-html/selection/</guid><content><![CDATA[&lt;h1 id=&#34;selection&#34;&gt;Selection&lt;/h1&gt;
&lt;p&gt;Represents a set of nodes in a DOM tree.&lt;/p&gt;
&lt;p&gt;Selections have a jQuery-compatible API, but with two caveats:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;CSS and screen layout are not processed, thus calls like css() and offset() are unavailable.&lt;/li&gt;
&lt;li&gt;DOM trees are read-only, you can&amp;rsquo;t set attributes or otherwise modify nodes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;(Note that the read-only nature of the DOM trees is purely to avoid a maintenance burden on code with seemingly no practical use - if a compelling use case is presented, modification can easily be implemented.)&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Method&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-attr/&#34;&gt;Selection.attr(name)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get the value of an attribute for the first element in the Selection.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-children/&#34;&gt;Selection.children([selector])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get the children of each element in the set of matched elements, optionally filtered by a selector.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-closest/&#34;&gt;Selection.closest(selector)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get the first element that matches the selector by testing the element itself and traversing up through its ancestors&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-contents/&#34;&gt;Selection.contents()&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get the children of each element in the set of matched elements, including text and comment nodes.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-data/&#34;&gt;Selection.data([key])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Return the value at the named data store for the first element in the set of matched elements.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-each/&#34;&gt;Selection.each(fn)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Iterate and execute a function for each matched element.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-eq/&#34;&gt;Selection.eq(index)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Reduce the set of matched elements to the one at the specified index.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-filter/&#34;&gt;Selection.filter(selector)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Reduce the set of matched elements to those that match the selector or pass the function&amp;rsquo;s test.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-find/&#34;&gt;Selection.find(selector)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Find the selection descendants, filtered by a selector.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-first/&#34;&gt;Selection.first()&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Reduce the set of matched elements to the first in the set.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-get/&#34;&gt;Selection.get(index)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Retrieve the 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/element/&#34;&gt;Element (k6/html)&lt;/a&gt; matched by the selector&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-has/&#34;&gt;Selection.has(selector)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Reduce the set of matched elements to those that have a descendant that matches the selector&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-html/&#34;&gt;Selection.html()&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get the HTML contents of the first element in the set of matched elements&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-is/&#34;&gt;Selection.is(selector)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Check the current matched set of elements against a selector or element and return true if at least one of these elements matches the given arguments.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-last/&#34;&gt;Selection.last()&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Reduce the set of matched elements to the final one in the set.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-map/&#34;&gt;Selection.map(fn)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Pass each selection in the current matched set through a function, producing a new Array containing the return values.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-nextall/&#34;&gt;Selection.nextAll([selector])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get all following siblings of each element in the set of matched elements, optionally filtered by a selector.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-next/&#34;&gt;Selection.next([selector])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get the immediately following sibling of each element in the set of matched element&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-nextuntil/&#34;&gt;Selection.nextUntil([selector], [filter])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get all following siblings of each element up to but not including the element matched by the selector.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-not/&#34;&gt;Selection.not(selector)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Remove elements from the set of matched elements&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-parent/&#34;&gt;Selection.parent([selector])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get the parent of each element in the current set of matched elements, optionally filtered by a selector.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-parents/&#34;&gt;Selection.parents([selector])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-parentsuntil/&#34;&gt;Selection.parentsUntil([selector], [filter])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get the ancestors of each element in the current set of matched elements, up to but not including the element matched by the selector.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-prevall/&#34;&gt;Selection.prevAll([selector])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-prev/&#34;&gt;Selection.prev([selector])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get the immediately preceding sibling of each element in the set of matched elements.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-prevuntil/&#34;&gt;Selection.prevUntil([selector], [filter])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get all preceding siblings of each element up to but not including the element matched by the selector.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-serialize/&#34;&gt;Selection.serialize()&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Encode a set of form elements as a string in standard URL-encoded notation for submission.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-serializearray/&#34;&gt;Selection.serializeArray()&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Encode a set of form elements as an array of names and values.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-serializeobject/&#34;&gt;Selection.serializeObject()&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Encode a set of form elements as an object.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-single/&#34;&gt;Selection.single(selector)&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Returns at most one element matching the selector.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-size/&#34;&gt;Selection.size()&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Return the number of elements in the Selection.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-slice/&#34;&gt;Selection.slice(start [, end])&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Reduce the set of matched elements to a subset specified by a range of indices.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-text/&#34;&gt;Selection.text()&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get the text content of the selection.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-toarray/&#34;&gt;Selection.toArray()&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Retrieve all the elements contained in the Selection, as an array.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-html/selection/selection-val/&#34;&gt;Selection.val()&lt;/a&gt;&lt;/td&gt;
              &lt;td&gt;Get the current value of the first element in the set of matched elements.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h3 id=&#34;example&#34;&gt;Example&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { parseHTML } from &amp;#39;k6/html&amp;#39;;
import http from &amp;#39;k6/http&amp;#39;;

export default function () {
  const res = http.get(&amp;#39;https://k6.io&amp;#39;);
  const doc = parseHTML(res.body);
  const pageTitle = doc.find(&amp;#39;head title&amp;#39;).text();
  const langAttr = doc.find(&amp;#39;html&amp;#39;).attr(&amp;#39;lang&amp;#39;);
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="selection">Selection&lt;/h1>
&lt;p>Represents a set of nodes in a DOM tree.&lt;/p>
&lt;p>Selections have a jQuery-compatible API, but with two caveats:&lt;/p>
&lt;ul>
&lt;li>CSS and screen layout are not processed, thus calls like css() and offset() are unavailable.&lt;/li>
&lt;li>DOM trees are read-only, you can&amp;rsquo;t set attributes or otherwise modify nodes.&lt;/li>
&lt;/ul>
&lt;p>(Note that the read-only nature of the DOM trees is purely to avoid a maintenance burden on code with seemingly no practical use - if a compelling use case is presented, modification can easily be implemented.)&lt;/p></description></item></channel></rss>