{"record":{"id":"d7b0018765f6507a","repo":"huggingface/smolagents","slug":"match-n-nth-result-not-found-only-len-elements","errorCode":null,"errorMessage":"Match n°{nth_result} not found (only {len(elements)} matches found)","messagePattern":"Match n°(.+?) not found \\(only (.+?) matches found\\)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/smolagents/vision_web_browser.py","lineNumber":116,"sourceCode":"        return f\"'{s}'\"\n    if '\"' not in s:\n        return f'\"{s}\"'\n    parts = s.split(\"'\")\n    return \"concat(\" + ', \"\\'\", '.join(f\"'{p}'\" for p in parts) + \")\"\n\n\n@tool\ndef search_item_ctrl_f(text: str, nth_result: int = 1) -> str:\n    \"\"\"\n    Searches for text on the current page via Ctrl + F and jumps to the nth occurrence.\n    Args:\n        text: The text to search for\n        nth_result: Which occurrence to jump to (default: 1)\n    \"\"\"\n    escaped_text = _escape_xpath_string(text)\n    elements = driver.find_elements(By.XPATH, f\"//*[contains(text(), {escaped_text})]\")\n    if nth_result > len(elements):\n        raise Exception(f\"Match n°{nth_result} not found (only {len(elements)} matches found)\")\n    result = f\"Found {len(elements)} matches for '{text}'.\"\n    elem = elements[nth_result - 1]\n    driver.execute_script(\"arguments[0].scrollIntoView(true);\", elem)\n    result += f\"Focused on element {nth_result} of {len(elements)}\"\n    return result\n\n\n@tool\ndef go_back() -> None:\n    \"\"\"Goes back to previous page.\"\"\"\n    driver.back()\n\n\n@tool\ndef close_popups() -> str:\n    \"\"\"\n    Closes any visible modal or pop-up on the page. Use this to dismiss pop-up windows! This does not work on cookie consent banners.\n    \"\"\"","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/vision_web_browser.py#L98-L134","documentation":"search_item_ctrl_f in smolagents' browser tools counts page elements containing the text via XPath contains(), and if nth_result exceeds the number of matches, raises this generic Exception naming the requested occurrence and the actual match count.","triggerScenarios":"Calling search_item_ctrl_f(driver, text, nth_result=5) when only 3 elements on the page contain the text; agent assuming more occurrences exist than the page has.","commonSituations":"Paginated or lazily loaded content where later matches appear only after scrolling; dynamic pages whose content changed between navigation and search; off-by-one requests from the LLM (asking for n+1).","solutions":["First call with nth_result=1 and read the reported match count, then request a valid index","Scroll or paginate to load all content before searching for later occurrences","Wait for the page/dynamic content to finish loading (WebDriverWait) before searching","Have the agent treat this error as a signal to re-search or navigate"],"exampleFix":"# before\nsearch_item_ctrl_f(driver, 'pricing', nth_result=7)\n\n# after\nres = search_item_ctrl_f(driver, 'pricing', nth_result=1)  # 'Found 3 matches'\nsearch_item_ctrl_f(driver, 'pricing', nth_result=min(7, 3))","handlingStrategy":"validation","validationCode":"def count_matches(driver, text):\n    from selenium.webdriver.common.by import By\n    escaped = text.replace('\\\"', '\\\\\"') if '\\\"' in text else f\\\"'{text}'\\\"\n    return len(driver.find_elements(By.XPATH, f'//*[contains(text(), {escaped])]'))\n\nif nth <= count_matches(driver, text):\n    search_item_ctrl_f(driver, text, nth_result=nth)","typeGuard":null,"tryCatchPattern":"try:\n    search_item_ctrl_f(driver, text, nth_result=n)\nexcept Exception as e:\n    if 'not found' in str(e):\n        # re-search with nth_result=1 to learn the count, then proceed\n        raise","preventionTips":["Probe with nth_result=1 and parse the reported match count","Scroll/wait for lazy-loaded content before searching later matches","Clamp nth to the known match count"],"tags":["smolagents","selenium","web-browser","dom-search"],"backgroundTag":"element-not-found","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}