{"record":{"id":"3416615e4eaca28e","repo":"code4craft/webmagic","slug":"links-can-not-apply-to-plain-text-please-check-wh","errorCode":null,"errorMessage":"Links can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).","messagePattern":"Links 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":47,"sourceCode":"\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\n    public List<Selectable> nodes() {\n        List<Selectable> nodes = new ArrayList<Selectable>(getSourceTexts().size());\n        for (String string : getSourceTexts()) {\n            nodes.add(PlainText.create(string));\n        }\n        return nodes;\n    }\n\n    @Override\n    protected List<String> getSourceTexts() {\n        return sourceTexts;\n    }\n}\n","sourceCodeStart":29,"sourceCodeEnd":64,"githubUrl":"https://github.com/code4craft/webmagic/blob/67816a19d68a4fec4657bf1336227e046e251df2/webmagic-core/src/main/java/us/codecraft/webmagic/selector/PlainText.java#L29-L64","documentation":"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.","triggerScenarios":"Calling .links() on a PlainText Selectable, e.g. page.getHtml().xpath(\"//div/@title\").links() or chaining links() after regex().","commonSituations":"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.","solutions":["Call links() directly on page.getHtml() before any text/attribute extraction","Replace links() with regex(\"<a ...href='([^']+)'\" ) style extraction on the plain text","Re-run the fetch and start a fresh chain from the Html object for link extraction"],"exampleFix":"// before\npage.getHtml().xpath(\"//div/@data-info\").links().all();\n// after\npage.getHtml().links().all();","handlingStrategy":"validation","validationCode":"if (selectable instanceof PlainText) { throw new IllegalStateException(\"cannot call links() on plain text\"); }","typeGuard":"boolean supportsLinks(Selectable s) { return !(s instanceof PlainText); }","tryCatchPattern":"try { links = selectable.links().all(); } catch (UnsupportedOperationException e) { links = selectable.regex(\"<a[^>]+href=\\\"([^\\\"]+)\\\"\").all(); }","preventionTips":["Call links() directly on page.getHtml()","Never chain links() after regex/attribute extraction","Add a SpiderListener to surface UnsupportedOperationException early in development"],"tags":["webmagic","links","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"}