code4craft/webmagic · error · UnsupportedOperationException

Links can not apply to plain text. Please check whether you…

Error message

Links can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).

What it means

PlainText.links() throws UnsupportedOperationException because link extraction requires parsing HTML anchor tags, and PlainText contains no markup. Like the other PlainText methods it points at a previous attribute select (/@href etc.) that converted the result to plain text.

Solutions

  1. Call links() directly on page.getHtml() before any text/attribute extraction
  2. Replace links() with regex("<a ...href='([^']+)'" ) style extraction on the plain text
  3. Re-run the fetch and start a fresh chain from the Html object for link extraction

Example fix

// before
page.getHtml().xpath("//div/@data-info").links().all();
// after
page.getHtml().links().all();
Defensive patterns

Strategy: validation

Validate before calling

if (selectable instanceof PlainText) { throw new IllegalStateException("cannot call links() on plain text"); }

Type guard

boolean supportsLinks(Selectable s) { return !(s instanceof PlainText); }

Try / catch

try { links = selectable.links().all(); } catch (UnsupportedOperationException e) { links = selectable.regex("<a[^>]+href=\"([^\"]+)\"").all(); }

Prevention

When it happens

Trigger: Calling .links() on a PlainText Selectable, e.g. page.getHtml().xpath("//div/@title").links() or chaining links() after regex().

Common situations: Crawler authors assuming links() can run anywhere in a chain; it only works on Html objects, so it fails after any step that returns plain strings.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of code4craft/webmagic@67816a19d6 (2026-09-08). Data as JSON: /api/errors/3416615e4eaca28e. Report an issue: GitHub.

Appendix: source

Thrown at webmagic-core/src/main/java/us/codecraft/webmagic/selector/PlainText.java:47

    @Override
    public Selectable xpath(String xpath) {
        throw new UnsupportedOperationException("XPath can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).");
    }

    @Override
    public Selectable $(String selector) {
		throw new UnsupportedOperationException("$ can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).");
    }

    @Override
    public Selectable $(String selector, String attrName) {
		throw new UnsupportedOperationException("$ can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).");
    }

    @Override
    public Selectable links() {
		throw new UnsupportedOperationException("Links can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).");
    }

    @Override
    public List<Selectable> nodes() {
        List<Selectable> nodes = new ArrayList<Selectable>(getSourceTexts().size());
        for (String string : getSourceTexts()) {
            nodes.add(PlainText.create(string));
        }
        return nodes;
    }

    @Override
    protected List<String> getSourceTexts() {
        return sourceTexts;
    }
}

View on GitHub (pinned to 67816a19d6)