searxng/searxng · error · SearxEngineXPathException
SearxEngineXPathException
Error message
SearxEngineXPathException
What it means
SearxEngineXPathException raised by eval_xpath when evaluating an already-compiled XPath against an element raises lxml XPathError — typically because the expression is evaluated on the wrong node type (e.g. an attribute node) or lxml runtime limits are hit, not a syntax error.
Source
Thrown at searx/utils.py:555
``TypeError``:
Raised when ``xpath_spec`` is neither a :py:obj:`str` nor a
:py:obj:`lxml.etree.XPath`.
``SearxXPathSyntaxException``:
Raised when there is a syntax error in the *XPath* selector (``str``).
``SearxEngineXPathException:``
Raised when the XPath can't be evaluated (masked
:py:obj:`lxml.etree..XPathError`).
"""
xpath: XPath = get_xpath(xpath_spec)
try:
# https://lxml.de/xpathxslt.html#xpath-return-values
return xpath(element)
except XPathError as e:
arg = ' '.join([str(i) for i in e.args])
raise SearxEngineXPathException(xpath_spec, arg) from e
def eval_xpath_list(element: ElementType, xpath_spec: XPathSpecType, min_len: int | None = None) -> list[t.Any]:
"""Same as :py:obj:`searx.utils.eval_xpath`, but additionally ensures the
return value is a :py:obj:`list`. The minimum length of the list is also
checked (if ``min_len`` is set)."""
result: list[t.Any] = eval_xpath(element, xpath_spec)
if not isinstance(result, list):
raise SearxEngineXPathException(xpath_spec, 'the result is not a list')
if min_len is not None and min_len > len(result):
raise SearxEngineXPathException(xpath_spec, 'len(xpath_str) < ' + str(min_len))
return result
def eval_xpath_getindex(
element: ElementType,
xpath_spec: XPathSpecType,View on GitHub (pinned to 9fea41204f)
Solutions
- Evaluate on element nodes: select '//a/@href' text separately but path expressions on elements
- Re-check the expression with SearxXPathSyntaxException causes ruled out — test directly: element.xpath(expr)
- Pin/upgrade lxml to a version compatible with your SearXNG release
Defensive patterns
Strategy: try-catch
Try / catch
try:
res = eval_xpath(dom, xp)
except SearxEngineXPathException as e:
logger.warning('xpath eval failed for %s: %s', xp, e)
res = None Prevention
- Evaluate path expressions against element nodes, not attribute/text nodes
- Test engines against saved HTML fixtures in CI
When it happens
Trigger: Calling eval_xpath(element, xpath) where xpath(element) raises XPathError, e.g. expressions expecting element context applied to attribute/text nodes, or very unusual evaluation failures inside lxml.
Common situations: Engine code evaluating element-relative paths on nodes obtained from attribute selection, or lxml version changes altering evaluation behavior on certain node types.
Related errors
AI-assisted analysis of searxng/searxng@9fea41204f (2026-08-27).
Data as JSON: /api/errors/722d7e31c3244100.
Report an issue: GitHub.