{"record":{"id":"894ae944d83a7c23","repo":"SeleniumHQ/selenium","slug":"target-type-target-type-not-found","errorCode":null,"errorMessage":"Target type '#{target_type}' not found","messagePattern":"Target type '#(.+?)' not found","errorType":"exception","errorClass":"Error::NoSuchTargetError","httpStatus":null,"severity":"error","filePath":"rb/lib/selenium/webdriver/devtools.rb","lineNumber":87,"sourceCode":"          define_method(method) do\n            Object.const_get(desired_class).new(self)\n          end\n        end\n\n        send(method)\n      end\n\n      def respond_to_missing?(method, *_args)\n        desired_class = \"Selenium::DevTools::V#{Selenium::DevTools.version}::#{method.capitalize}\"\n        Object.const_defined?(desired_class)\n      end\n\n      private\n\n      def start_session(target_type:)\n        targets = target.get_targets.dig('result', 'targetInfos')\n        found_target = targets.find { |target| target['type'] == target_type }\n        raise Error::NoSuchTargetError, \"Target type '#{target_type}' not found\" unless found_target\n\n        session = target.attach_to_target(target_id: found_target['targetId'], flatten: true)\n        @session_id = session.dig('result', 'sessionId')\n      end\n\n      def error_message(error)\n        [error['code'], error['message'], error['data']].join(': ')\n      end\n    end # DevTools\n  end # WebDriver\nend # Selenium\n","sourceCodeStart":69,"sourceCodeEnd":99,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/rb/lib/selenium/webdriver/devtools.rb#L69-L99","documentation":"Raised as Error::NoSuchTargetError during DevTools session startup (start_session) when none of the targets returned by Target.getTargets match the requested target_type. The DevTools endpoint enumerates available browser targets and attaches to the first whose 'type' equals the requested value; if none matches, there is no target to attach to.","triggerScenarios":"Initializing driver.devtools (or DevTools.new) with a target_type that does not exist among current targets, e.g. 'page' when no tab/page target is open, or a misspelled/unsupported type like 'tab' or 'iframe'.","commonSituations":"Connecting DevTools to a browser whose only targets are background/service-worker types; calling devtools before navigating to any page; passing a non-standard target_type string; version mismatches where the browser exposes different target categories.","solutions":["Ensure a page target exists before connecting DevTools (open/navigate a tab first).","Pass a target_type that matches an actual returned target type (commonly 'page'); inspect available targets via the CDP Target.getTargets to confirm.","If attaching to a non-page target, verify the browser version supports that target type and that such a target is currently live."],"exampleFix":"// before\ndevtools = driver.devtools  # fails if no 'page' target present\n\n// after\ndriver.get('https://example.com')  # ensure a page target exists first\ndevtools = driver.devtools","handlingStrategy":"validation","validationCode":"# Ensure a page target exists before connecting DevTools\ndriver.get(base_url) unless driver.window_handles.any?\nbegin\n  devtools = driver.devtools\nrescue Selenium::WebDriver::Error::NoSuchTargetError => e\n  raise \"No DevTools target available: #{e.message}\"\nend","typeGuard":null,"tryCatchPattern":"begin\n  devtools = driver.devtools\nrescue Selenium::WebDriver::Error::NoSuchTargetError => e\n  WebDriver.logger.warn(\"DevTools attach failed: #{e.message}\")\n  retry_after_opening_page\nend","preventionTips":["Open/navigate to a page before calling driver.devtools so a 'page' target exists.","Use a target_type you have confirmed exists via Target.getTargets.","Guard DevTools-dependent code with a rescue on NoSuchTargetError for headless/background-target scenarios."],"tags":["devtools","cdp","target","session"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}