{"record":{"id":"1921cb7a5dda2d6d","repo":"NaiboWang/EasySpider","slug":"you-cannot-reuse-the-chromeoptions-object","errorCode":null,"errorMessage":"you cannot reuse the ChromeOptions object","messagePattern":"you cannot reuse the ChromeOptions object","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"ExecuteStage/undetected_chromedriver_ES/__init__.py","lineNumber":268,"sourceCode":"        self.patcher = Patcher(\n            executable_path=driver_executable_path,\n            force=patcher_force_close,\n            version_main=version_main,\n            user_multi_procs=user_multi_procs,\n        )\n        # self.patcher.auto(user_multiprocess = user_multi_num_procs)\n        chrome_version = self.patcher.auto()\n\n        # self.patcher = patcher\n        if not options:\n            options = ChromeOptions()\n\n        try:\n            if hasattr(options, \"_session\") and options._session is not None:\n                #  prevent reuse of options,\n                #  as it just appends arguments, not replace them\n                #  you'll get conflicts starting chrome\n                raise RuntimeError(\"you cannot reuse the ChromeOptions object\")\n        except AttributeError:\n            pass\n\n        options._session = self\n\n        if not options.debugger_address:\n            debug_port = (\n                port\n                if port != 0\n                else selenium.webdriver.common.service.utils.free_port()\n            )\n            debug_host = \"127.0.0.1\"\n            options.debugger_address = \"%s:%d\" % (debug_host, debug_port)\n        else:\n            debug_host, debug_port = options.debugger_address.split(\":\")\n            debug_port = int(debug_port)\n\n        if enable_cdp_events:","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/NaiboWang/EasySpider/blob/191bd6d7547bb397e4c579dd2c70ae835be3f512/ExecuteStage/undetected_chromedriver_ES/__init__.py#L250-L286","documentation":"Raised as RuntimeError by undetected_chromedriver's Chrome.__init__ (ExecuteStage/undetected_chromedriver_ES/__init__.py:268) when the SAME ChromeOptions instance is passed to a second Chrome(...) constructor. On first use UC stamps options._session = self; a non-None _session on a later call means the options object is being reused, which would accumulate duplicate --remote-debugging-port / --user-data-dir arguments and conflict at startup.","triggerScenarios":"Construct uc.Chrome(options=opts); later construct uc.Chrome(options=opts) again with the same opts object. The check `hasattr(options, '_session') and options._session is not None` is true on the second call, so RuntimeError is raised before any chrome process starts.","commonSituations":"Loop/factory code creates one ChromeOptions outside a loop then builds a driver per iteration; retry/reconnect logic reuses the original options; tests spin up a fresh driver per case but share a module-level options constant.","solutions":["Construct a brand-new ChromeOptions() for every uc.Chrome() call (move the options build inside the loop/factory).","If you need identical settings, wrap the options-building in a helper function and call it each time.","Do NOT reset opts._session = None to bypass the guard - the accumulated arguments will still conflict.","If migrating from vanilla selenium where options reuse was tolerated, audit every call site."],"exampleFix":"# before\nopts = uc.ChromeOptions()\nopts.add_argument('--disable-gpu')\nfor _ in range(3):\n    driver = uc.Chrome(options=opts)  # 2nd+ iteration throws\n\n# after\ndef make_opts():\n    o = uc.ChromeOptions()\n    o.add_argument('--disable-gpu')\n    return o\nfor _ in range(3):\n    driver = uc.Chrome(options=make_opts())  # fresh each time","handlingStrategy":"validation","validationCode":"def options_is_consumed(opts) -> bool:\n    return getattr(opts, '_session', None) is not None\n\nif options_is_consumed(opts):\n    opts = uc.ChromeOptions()  # rebuild before passing to Chrome()","typeGuard":null,"tryCatchPattern":"import undetected_chromedriver_ES as uc\ntry:\n    driver = uc.Chrome(options=opts)\nexcept RuntimeError as e:\n    if 'cannot reuse the ChromeOptions' in str(e):\n        opts = uc.ChromeOptions()  # rebuild and retry once\n        driver = uc.Chrome(options=opts)\n    else:\n        raise","preventionTips":["Never hoist a single ChromeOptions to module scope for reuse across drivers.","In retry/reconnect paths, rebuild options alongside the driver.","Add a unit test that asserts two drivers can be created from two sibling options objects."],"tags":["undetected-chromedriver","chromeoptions","configuration","python"],"backgroundTag":null,"analyzedSha":"191bd6d7547bb397e4c579dd2c70ae835be3f512","analyzedAt":"2026-08-13T03:11:17.041Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}