jhy/jsoup · error · Selector.SelectorParseException

Could not parse query

Error message

Could not parse query '%s': unknown node type '::%s'

What it means

jsoup's node selector parser (:: syntax, e.g. ::text, ::comment, ::data, ::cdata) threw because the identifier after '::' is not one of the known node types. The Selector.SelectorParseException names the query and the unknown node type. Only a fixed set of node-type keywords is supported.

Solutions

  1. Use a supported node type: ::text, ::comment, ::data, ::cdata
  2. Fix typos in the node selector (e.g. '::coment' -> '::comment')
  3. Combine node selectors with subclasses if needed, e.g. '::comment:contains(foo)'
  4. Use Class-based selection, e.g. selectNodes(query, TextNode.class) with ::text

Example fix

// before
List<Comment> c = doc.selectNodes("div::coment", Comment.class);
// after
List<Comment> c = doc.selectNodes("div::comment", Comment.class);
Defensive patterns

Strategy: validation

Validate before calling

if (!java.util.regex.Pattern.compile("::(text|comment|data|cdata)").matcher(query).find() && query.contains("::")) {
    throw new IllegalArgumentException("Unknown ::node type in: " + query);
}

Type guard

boolean isKnownNodeSelector(String q) { return q.matches(".*::(text|comment|data|cdata).*"); }

Try / catch

try {
    List<TextNode> nodes = doc.selectNodes(query, TextNode.class);
} catch (Selector.SelectorParseException e) {
    log.error("Unknown node type in '{}'", query);
    throw e;
}

Prevention

When it happens

Trigger: A selector containing '::unknownType', e.g. doc.select("div::elements") or a typo like doc.select("::coment"). Parsed in QueryParser.parseNodeSelector via parseSubclass.

Common situations: Typos in node selectors; guessing at supported ::node names; porting queries written for other selector engines with different node pseudo-syntax.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of jhy/jsoup@9851ac5d9c (2026-09-08). Data as JSON: /api/errors/e8c3ab18095a1971. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/org/jsoup/select/QueryParser.java:292

                left = new NodeEvaluator.InstanceType(Node.class, pseudo);
                break;
            case "leafnode":
                left = new NodeEvaluator.InstanceType(LeafNode.class, pseudo);
                break;
            case "text":
                left = new NodeEvaluator.InstanceType(TextNode.class, pseudo);
                break;
            case "comment":
                left = new NodeEvaluator.InstanceType(Comment.class, pseudo);
                break;
            case "data":
                left = new NodeEvaluator.InstanceType(DataNode.class, pseudo);
                break;
            case "cdata":
                left = new NodeEvaluator.InstanceType(CDataNode.class, pseudo);
                break;
            default:
                throw new Selector.SelectorParseException(
                    "Could not parse query '%s': unknown node type '::%s'", query, pseudo);
        }

        // Handle following subclasses in node context (like ::comment:contains())
        Evaluator right;
        while ((right = parseSubclass()) != null) {
            left = and(left, right);
        }

        inNodeContext = false;
        return left;
    }

    private Evaluator byId() {
        String id = tq.consumeCssIdentifier();
        Validate.notEmpty(id);
        return new Evaluator.Id(id);
    }

View on GitHub (pinned to 9851ac5d9c)