JavaScript SDK: Dom: searching HTML
ctx.dom(html, baseUrl) or response.dom() parses an already retrieved page. CSS selectors search the local document without another request.
Each example is a separate fragment inside async function run(ctx). Use await only for methods whose signature returns a Promise.
Obtaining the object
const dom = ctx.dom('<a href="/product">Product</a>', 'https://example.com');
Methods
q
dom.q(css) → Node | null
Find the first HTML element matching a CSS selector.
const result = dom.q('a');
qa
dom.qa(css) → Array<Node>
Find all matching HTML elements.
const result = dom.qa('a');
first
dom.first(cascade) → Node | null
Try several selectors and take the first match found.
const result = dom.first(['.product', 'article']);
text
dom.text(css, default) → string | null
Get the first match's text without an intermediate Node variable.
const result = dom.text('.product-title');
attr
dom.attr(css, attribute, default) → string | null
Read an attribute from the first matching element.
const result = dom.attr('a', 'href');
maxNumber
dom.maxNumber(css) → number | null
Find the largest number in element text, such as the last page number.
const result = dom.maxNumber('a');
numbersByPattern
dom.numbersByPattern(pattern, subject) → Array<unknown>
Finds numbers using a JavaScript regular expression. It uses the first capture group when present, otherwise the complete match.
const result = dom.numbersByPattern('(\\d+)', 'Price 1250');