{"record":{"id":"f015ed8da875edc8","repo":"unclecode/crawl4ai","slug":"invalid-css-selector-no-elements-found-for-css-se","errorCode":null,"errorMessage":"Invalid CSS selector, No elements found for CSS selector: {css_selector}","messagePattern":"Invalid CSS selector, No elements found for CSS selector: (.+?)","errorType":"exception","errorClass":"InvalidCSSSelectorError","httpStatus":null,"severity":"error","filePath":"crawl4ai/utils.py","lineNumber":1184,"sourceCode":"    \"\"\"\n    if not html:\n        return None\n\n    soup = BeautifulSoup(html, \"html.parser\")\n    body = soup.body\n\n    image_description_min_word_threshold = kwargs.get(\n        \"image_description_min_word_threshold\", IMAGE_DESCRIPTION_MIN_WORD_THRESHOLD\n    )\n\n    for tag in kwargs.get(\"excluded_tags\", []) or []:\n        for el in body.select(tag):\n            el.decompose()\n\n    if css_selector:\n        selected_elements = body.select(css_selector)\n        if not selected_elements:\n            raise InvalidCSSSelectorError(\n                f\"Invalid CSS selector, No elements found for CSS selector: {css_selector}\"\n            )\n        body = soup.new_tag(\"div\")\n        for el in selected_elements:\n            body.append(el)\n\n    links = {\"internal\": [], \"external\": []}\n    media = {\"images\": [], \"videos\": [], \"audios\": []}\n\n    # Extract meaningful text for media files from closest parent\n    def find_closest_parent_with_useful_text(tag):\n        current_tag = tag\n        while current_tag:\n            current_tag = current_tag.parent\n            # Get the text content from the parent tag\n            if current_tag:\n                text_content = current_tag.get_text(separator=\" \", strip=True)\n                # Check if the text content has at least word_count_threshold","sourceCodeStart":1166,"sourceCodeEnd":1202,"githubUrl":"https://github.com/unclecode/crawl4ai/blob/7e801521428ee12509994d39151006f64055ebe3/crawl4ai/utils.py#L1166-L1202","documentation":"Raised as InvalidCSSSelectorError by get_content_of_website_optimized (crawl4ai/utils.py:1184) when the css_selector argument matches no elements after excluded_tags decomposition. Distinct from 140/141 in that it fires in the 'optimized' extraction path, and importantly the exclusion step runs first — so a selector can legitimately match in the raw HTML but still fail because excluded_tags decomposed the matched nodes beforehand.","triggerScenarios":"Calling get_content_of_website_optimized(url, html, css_selector=\"nav.menu\") with excluded_tags=[\"nav\"] (the selector's own match is decomposed); a selector matching only <script>/<form> nodes when those tags are excluded; same no-match cases as 140 on optimized path.","commonSituations":"Passing excluded_tags that overlap the css_selector target; configuration drift between the exclusion list and the extraction selector; SPA static-HTML mismatch as in 140.","solutions":["Check for overlap between css_selector and excluded_tags and remove the conflict.","Confirm the selector matches in the exact HTML passed (static markup, pre-JS).","Pre-validate with BeautifulSoup: BeautifulSoup(html,'html.parser').body.select(sel) before calling the API.","Catch InvalidCSSSelectorError and retry without css_selector to get full-page content as a fallback."],"exampleFix":"# before\nres = get_content_of_website_optimized(url, html, css_selector=\"div.ads + div.content\", excluded_tags=[\"div\"])\n\n# after\nres = get_content_of_website_optimized(url, html, css_selector=\"div.content\", excluded_tags=[\"script\", \"style\"])","handlingStrategy":"validation","validationCode":"def safe_extract_optimized(html, css_selector, excluded_tags):\n    if css_selector:\n        sel_soup = BeautifulSoup(html, \"html.parser\")\n        for tag in excluded_tags or []:\n            for el in sel_soup.select(tag):\n                el.decompose()\n        if not (sel_soup.body or sel_soup).select(css_selector):\n            return None  # would raise; caller falls back\n    return get_content_of_website_optimized(\n        \"local\", html, css_selector=css_selector, excluded_tags=excluded_tags\n    )","typeGuard":null,"tryCatchPattern":"try:\n    res = get_content_of_website_optimized(url, html, css_selector=sel, excluded_tags=ex)\nexcept InvalidCSSSelectorError:\n    res = get_content_of_website_optimized(url, html, excluded_tags=ex)","preventionTips":["Never let excluded_tags overlap the css_selector target","Simulate exclusion before extraction when tags and selectors come from separate config"],"tags":["css-selector","excluded-tags","crawl4ai","beautifulsoup"],"backgroundTag":null,"analyzedSha":"7e801521428ee12509994d39151006f64055ebe3","analyzedAt":"2026-08-14T20:46:20.673Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}