{"record":{"id":"df6d96476a8e39b3","repo":"code4craft/webmagic","slug":"xpath-can-not-apply-to-plain-text-please-check-wh","errorCode":null,"errorMessage":"XPath can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).","messagePattern":"XPath can not apply to plain text\\. Please check whether you use a previous xpath with attribute select \\(/@href etc\\)\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"webmagic-core/src/main/java/us/codecraft/webmagic/selector/PlainText.java","lineNumber":32,"sourceCode":"\n    protected List<String> sourceTexts;\n\n    public PlainText(List<String> sourceTexts) {\n        this.sourceTexts = sourceTexts;\n    }\n\n    public PlainText(String text) {\n        this.sourceTexts = new ArrayList<String>();\n        sourceTexts.add(text);\n    }\n\n    public static PlainText create(String text) {\n        return new PlainText(text);\n    }\n\n    @Override\n    public Selectable xpath(String xpath) {\n        throw new UnsupportedOperationException(\"XPath can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).\");\n    }\n\n    @Override\n    public Selectable $(String selector) {\n\t\tthrow new UnsupportedOperationException(\"$ can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).\");\n    }\n\n    @Override\n    public Selectable $(String selector, String attrName) {\n\t\tthrow new UnsupportedOperationException(\"$ can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).\");\n    }\n\n    @Override\n    public Selectable links() {\n\t\tthrow new UnsupportedOperationException(\"Links can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).\");\n    }\n\n    @Override","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/code4craft/webmagic/blob/67816a19d68a4fec4657bf1336227e046e251df2/webmagic-core/src/main/java/us/codecraft/webmagic/selector/PlainText.java#L14-L50","documentation":"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.","triggerScenarios":"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.","commonSituations":"Crawlers built with WebMagic chaining selectors: page.getHtml().links().xpath(...) or links().regex(...) returns plain strings; developer mistakenly continues with xpath expecting HtmlNode semantics.","solutions":["Remove the xpath() call or reorder selectors so xpath runs on Html (node) results before any attribute/text extraction that yields PlainText","Use regex() or replace() selectors on PlainText results instead of xpath","Re-select from page.getHtml() (Html object) for the xpath part instead of chaining on the plain-text result"],"exampleFix":"// before\nList<String> urls = page.getHtml().links().xpath(\"//div/@href\").all();\n// after\nList<String> urls = page.getHtml().xpath(\"//div/a/@href\").all();","handlingStrategy":"validation","validationCode":"if (selectable instanceof PlainText) { throw new IllegalStateException(\"cannot call xpath on plain text\"); }","typeGuard":"boolean supportsXpath(Selectable s) { return !(s instanceof PlainText); }","tryCatchPattern":"try { result = selectable.xpath(expr); } catch (UnsupportedOperationException e) { log.warn(\"selector chain reduced to plain text\", e); result = fallback; }","preventionTips":["Apply xpath only to page.getHtml() results before attribute/text extraction","Avoid chaining xpath after /@href, links(), or regex() steps","Know the Selectable type each selector returns"],"tags":["webmagic","xpath","unsupported-operation","selector-chain"],"backgroundTag":"unsupported-operation","analyzedSha":"67816a19d68a4fec4657bf1336227e046e251df2","analyzedAt":"2026-09-08T11:15:28.425Z","contentChangedAt":"2026-09-08T11:15:28.425Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}