{"record":{"id":"7a77811e059fed77","repo":"unclecode/crawl4ai","slug":"invalid-css-selector-css-selector-7a7781","errorCode":null,"errorMessage":"Invalid CSS selector: {css_selector}","messagePattern":"Invalid CSS selector: (.+?)","errorType":"exception","errorClass":"InvalidCSSSelectorError","httpStatus":null,"severity":"error","filePath":"crawl4ai/utils.py","lineNumber":1140,"sourceCode":"        try:\n            meta = extract_metadata(html, soup)\n        except Exception as e:\n            print(\"Error extracting metadata:\", str(e))\n            meta = {}\n\n        # Return the Markdown content\n        return {\n            \"markdown\": markdown,\n            \"cleaned_html\": cleaned_html,\n            \"success\": True,\n            \"media\": media,\n            \"links\": links,\n            \"metadata\": meta,\n        }\n\n    except Exception as e:\n        print(\"Error processing HTML content:\", str(e))\n        raise InvalidCSSSelectorError(f\"Invalid CSS selector: {css_selector}\") from e\n\n\ndef get_content_of_website_optimized(\n    url: str,\n    html: str,\n    word_count_threshold: int = MIN_WORD_THRESHOLD,\n    css_selector: str = None,\n    **kwargs,\n) -> Dict[str, Any]:\n    \"\"\"\n    Extracts and cleans content from website HTML, optimizing for useful media and contextual information.\n    \n    Parses the provided HTML to extract internal and external links, filters and scores images for usefulness, gathers contextual descriptions for media, removes unwanted or low-value elements, and converts the cleaned HTML to Markdown. Also extracts metadata and returns all structured content in a dictionary.\n    \n    Args:\n        url: The URL of the website being processed.\n        html: The raw HTML content to extract from.\n        word_count_threshold: Minimum word count for elements to be retained.","sourceCodeStart":1122,"sourceCodeEnd":1158,"githubUrl":"https://github.com/unclecode/crawl4ai/blob/7e801521428ee12509994d39151006f64055ebe3/crawl4ai/utils.py#L1122-L1158","documentation":"This InvalidCSSSelectorError (crawl4ai/utils.py:1140) is a catch-all re-raise at the end of an except Exception block in the HTML-processing helper: ANY exception raised while cleaning/extracting content (not just selector failures) is printed and re-raised as InvalidCSSSelectorError with the chained cause. So the real failure may be unrelated to CSS selectors — inspect the 'from e' __cause__ and the printed 'Error processing HTML content:' line.","triggerScenarios":"Any exception inside the try block of this helper: a css_selector that triggers a BeautifulSoup SelectorSyntaxError; unexpected None from a malformed document causing an AttributeError; or the underlying no-elements match condition — all surface as this same error. Calling the function with an exotic selector like \":has-text('foo')\" (not supported by BeautifulSoup) hits it immediately.","commonSituations":"Developers assume the error means the selector is wrong and rewrite it repeatedly when the actual cause (visible in the chained exception) is something else entirely, e.g. a NoneType error or an unsupported pseudo-class; using Playwright-style selectors (::text, >>, :has-text) with the BeautifulSoup-based utility.","solutions":["Read the full traceback and the chained cause (raise ... from e) — the printed 'Error processing HTML content:' message names the real exception.","If the cause is a SelectorSyntaxError, replace Playwright/Selenium-specific selector syntax with standard CSS supported by BeautifulSoup (soupsieve).","If the cause is the no-elements case, fix the selector per error 140 guidance.","If the cause is malformed HTML, pre-clean with BeautifulSoup(html, 'html.parser').prettify() or pass a parser that tolerates the input."],"exampleFix":"// before\ntry:\n    out = process_html(html, css_selector=\"a >> text=foo\")\nexcept InvalidCSSSelectorError:\n    pass  # real cause hidden\n\n// after\ntry:\n    out = process_html(html, css_selector=\"a.foo\")\nexcept InvalidCSSSelectorError as e:\n    logging.error(\"underlying cause: %r\", e.__cause__)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    out = process_html(html, css_selector=sel)\nexcept InvalidCSSSelectorError as e:\n    cause = e.__cause__\n    if \"SelectorSyntaxError\" in type(cause).__name__:\n        fix_selector()      # unsupported pseudo-class syntax\n    else:\n        inspect(cause)      # real error is chained, not selector-related","preventionTips":["Always inspect __cause__ on this error — the wrapper masks the real exception","Restrict selectors to soupsieve-supported CSS (no ::text, >>, :has-text)","Check the server/console print 'Error processing HTML content:' for the true message"],"tags":["css-selector","error-masking","crawl4ai","chained-exception"],"backgroundTag":null,"analyzedSha":"7e801521428ee12509994d39151006f64055ebe3","analyzedAt":"2026-08-14T20:46:20.673Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}