{"record":{"id":"8e8c42bf7c4ae708","repo":"SeleniumHQ/selenium","slug":"unable-to-encode-string-to-utf-8-e-message-st","errorCode":null,"errorMessage":"Unable to encode string to UTF-8: #{e.message}. String encoding: #{str.encoding}, content: #{str.inspect}","messagePattern":"Unable to encode string to UTF-8: #(.+?)\\. String encoding: #(.+?), content: #(.+?)","errorType":"exception","errorClass":"Error::WebDriverError","httpStatus":null,"severity":"error","filePath":"rb/lib/selenium/webdriver/remote/http/common.rb","lineNumber":134,"sourceCode":"              obj.each_with_object({}) do |(key, value), result|\n                result[ensure_utf8_encoding(key)] = ensure_utf8_encoding(value)\n              end\n            else\n              obj\n            end\n          end\n\n          def encode_string_to_utf8(str)\n            return str if str.encoding == Encoding::UTF_8 && str.valid_encoding?\n\n            if BINARY_ENCODINGS.include?(str.encoding)\n              result = str.dup.force_encoding(Encoding::UTF_8)\n              return result if result.valid_encoding?\n            end\n\n            str.encode(Encoding::UTF_8)\n          rescue EncodingError => e\n            raise Error::WebDriverError,\n                  \"Unable to encode string to UTF-8: #{e.message}. \" \\\n                  \"String encoding: #{str.encoding}, content: #{str.inspect}\"\n          end\n\n          def create_response(code, body, content_type)\n            code = code.to_i\n            body = body.to_s.strip\n            content_type = content_type.to_s\n            WebDriver.logger.debug(\"<- #{body}\", id: :command)\n\n            if content_type.include? CONTENT_TYPE\n              raise Error::WebDriverError, \"empty body: #{content_type.inspect} (#{code})\\n#{body}\" if body.empty?\n\n              Response.new(code, JSON.parse(body))\n            elsif code == 204\n              Response.new(code)\n            else\n              msg = if body.empty?","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/rb/lib/selenium/webdriver/remote/http/common.rb#L116-L152","documentation":"Raised as Error::WebDriverError by Http::Common#encode_string_to_utf8 when the response body string cannot be converted to UTF-8. The method first checks for already-valid UTF-8, then tries force_encoding for binary encodings, then str.encode(UTF_8); if all fail with an EncodingError, it wraps the failure with the original encoding and content for diagnostics.","triggerScenarios":"The remote server returns a response body in an encoding incompatible with UTF-8 (e.g. a legacy single-byte encoding with invalid byte sequences). A response containing binary garbage or a truncated multi-byte sequence. Content with mixed encodings concatenated server-side that produces invalid UTF-8 when forced.","commonSituations":"Scraping or interacting with pages whose server responses include non-UTF-8 characters in error messages or alert text. Remote nodes running on OSes with non-UTF-8 default encodings. Proxy or middleware corrupting response bodies. Browser crashes producing binary heap dumps in the response body.","solutions":["Check the remote server and browser locale/encoding settings; configure them to use UTF-8.","If the error is intermittent, capture the raw response via debug logging (id: :command) to identify the problematic byte sequence.","Update the Selenium Server/Grid to a version that normalizes response encodings.","If the content is genuinely non-text (e.g. a screenshot or crash dump), verify you are calling the correct endpoint."],"exampleFix":"# No caller-side code fix; this is a server/network encoding issue.\n# Enable logging to diagnose:\n# Selenium::WebDriver::WebDriver.logger.level = :debug\n# Then inspect the logged \"<- ...\" response body for encoding issues.","handlingStrategy":"try-catch","validationCode":"# No caller-side prevention; this depends on server response encoding.\n# You can enable debug logging to capture responses for diagnosis:\n# Selenium::WebDriver.logger.level = :debug","typeGuard":null,"tryCatchPattern":"begin\n  driver.navigate.to(url)\nrescue Selenium::WebDriver::Error::WebDriverError => e\n  raise unless e.message.include?('Unable to encode string to UTF-8')\n  WebDriver.logger.error(\"Encoding error from server: #{e.message}\", id: :command)\n  retry\nend","preventionTips":["Configure the remote server and browser to use UTF-8 locale/encoding.","Enable debug logging to capture the problematic response for diagnosis.","Keep the Selenium Server/Grid updated to benefit from encoding normalization fixes."],"tags":["encoding","utf-8","http-response","serialization"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}