Skip to main content

Module selectors

Module selectors 

Source
Expand description

CSS selector sanitization for LLM-generated selectors. CSS selector sanitization and validation for LLM-generated selectors.

The LLM sometimes returns garbage instead of a CSS selector (explanations, JSON objects, :not(*), null, stray quotes or code fences). These helpers normalize the response and reject values that can never match an element, so step errors show the LLM’s actual output instead of a cryptic “element :not(*) not found”.

Functions§

sanitize_selector
Strips common LLM response noise from a raw selector string: surrounding whitespace, code fences (with optional language tag), and outer single/double/backtick quotes.
selector_is_useless
Returns true when a sanitized selector can never match a real element, i.e. the LLM clearly did not produce a selector at all.
selector_matches_js
Builds the JavaScript snippet that checks whether a selector matches at least one element on the current page.
validate_selector
Basic syntactic sanity check for a selector before it is executed.