SeleniumHQ/selenium · error · Error::NoSuchTargetError
Target type '#{target_type}' not found
Error message
Target type '#{target_type}' not found What it means
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.
Source
Thrown at rb/lib/selenium/webdriver/devtools.rb:87
define_method(method) do
Object.const_get(desired_class).new(self)
end
end
send(method)
end
def respond_to_missing?(method, *_args)
desired_class = "Selenium::DevTools::V#{Selenium::DevTools.version}::#{method.capitalize}"
Object.const_defined?(desired_class)
end
private
def start_session(target_type:)
targets = target.get_targets.dig('result', 'targetInfos')
found_target = targets.find { |target| target['type'] == target_type }
raise Error::NoSuchTargetError, "Target type '#{target_type}' not found" unless found_target
session = target.attach_to_target(target_id: found_target['targetId'], flatten: true)
@session_id = session.dig('result', 'sessionId')
end
def error_message(error)
[error['code'], error['message'], error['data']].join(': ')
end
end # DevTools
end # WebDriver
end # Selenium
View on GitHub (pinned to aa36b38e69)
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.
Example fix
// before
devtools = driver.devtools # fails if no 'page' target present
// after
driver.get('https://example.com') # ensure a page target exists first
devtools = driver.devtools Defensive patterns
Strategy: validation
Validate before calling
# Ensure a page target exists before connecting DevTools
driver.get(base_url) unless driver.window_handles.any?
begin
devtools = driver.devtools
rescue Selenium::WebDriver::Error::NoSuchTargetError => e
raise "No DevTools target available: #{e.message}"
end Try / catch
begin
devtools = driver.devtools
rescue Selenium::WebDriver::Error::NoSuchTargetError => e
WebDriver.logger.warn("DevTools attach failed: #{e.message}")
retry_after_opening_page
end Prevention
- 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.
When it happens
Trigger: 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'.
Common situations: 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.
Related errors
- {fn_name}() must be called in a session context.
- CDP devtools module not loaded. Call import_devtools() first
- CDP version not found in capabilities
- Unable to find url to connect to from capabilities
- CDP module not loaded
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/894ae944d83a7c23.
Report an issue: GitHub.