diff --git a/lib/ferrum/client.rb b/lib/ferrum/client.rb index 1039a475..7025a9fd 100644 --- a/lib/ferrum/client.rb +++ b/lib/ferrum/client.rb @@ -165,7 +165,7 @@ def raise_browser_error(error) "Inspected target navigated or closed" raise NodeNotFoundError, error # Context is lost, page is reloading - when "Cannot find context with specified id" + when "Cannot find context with specified id", /Failed to find context with id/ raise NoExecutionContextError, error when "No target with given id found" raise NoSuchPageError diff --git a/lib/ferrum/contexts.rb b/lib/ferrum/contexts.rb index 9c5fb984..0cb698ad 100644 --- a/lib/ferrum/contexts.rb +++ b/lib/ferrum/contexts.rb @@ -5,6 +5,7 @@ module Ferrum class Contexts ALLOWED_TARGET_TYPES = %w[page iframe].freeze + ABOUT_BLANK = "about:blank" include Enumerable @@ -49,8 +50,15 @@ def create(**options) def dispose(context_id) context = @contexts[context_id] context.close_targets_connection - @client.command("Target.disposeBrowserContext", browserContextId: context.id) - @contexts.delete(context_id) + + begin + @client.command("Target.disposeBrowserContext", browserContextId: context.id) + rescue NoExecutionContextError + warn "Browser context #{context.id} was already disposed" unless context.page&.url == ABOUT_BLANK + ensure + @contexts.delete(context_id) + end + true end