code4craft/webmagic · error · UnsupportedOperationException

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

Error message

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

What it means

PlainText is a Selectable that wraps extracted plain text rather than parsed HTML/XML. XPath queries require a DOM, so PlainText deliberately throws UnsupportedOperationException from its xpath() method. This usually means a previous selector (e.g. an attribute select like /@href) already converted the result to plain text and you then chained an xpath call on it.

Solutions

  1. Remove the xpath() call or reorder selectors so xpath runs on Html (node) results before any attribute/text extraction that yields PlainText
  2. Use regex() or replace() selectors on PlainText results instead of xpath
  3. Re-select from page.getHtml() (Html object) for the xpath part instead of chaining on the plain-text result

Example fix

// before
List<String> urls = page.getHtml().links().xpath("//div/@href").all();
// after
List<String> urls = page.getHtml().xpath("//div/a/@href").all();
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try { result = selectable.xpath(expr); } catch (UnsupportedOperationException e) { log.warn("selector chain reduced to plain text", e); result = fallback; }

Prevention

When it happens

Trigger: Calling .xpath(...) on a Selectable whose runtime type is PlainText — typically chaining xpath after an attribute extraction such as $('a/@href').xpath(...), or after regex()/css() steps that returned PlainText.

Common situations: Crawlers built with WebMagic chaining selectors: page.getHtml().links().xpath(...) or links().regex(...) returns plain strings; developer mistakenly continues with xpath expecting HtmlNode semantics.

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/df6d96476a8e39b3. Report an issue: GitHub.

Appendix: source

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

    protected List<String> sourceTexts;

    public PlainText(List<String> sourceTexts) {
        this.sourceTexts = sourceTexts;
    }

    public PlainText(String text) {
        this.sourceTexts = new ArrayList<String>();
        sourceTexts.add(text);
    }

    public static PlainText create(String text) {
        return new PlainText(text);
    }

    @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

View on GitHub (pinned to 67816a19d6)