SeleniumHQ/selenium · error · ArgumentError
could not convert #{str.inspect} into color
Error message
could not convert #{str.inspect} into color What it means
Support::Color.from_string parses a CSS color string by trying a fixed set of regexes (rgb, rgb%, rgba, rgba%, #rrggbb, #rgb, hsl, hsla). If none match it raises ArgumentError. Crucially, CSS named colors ('red', 'blue', 'transparent') and modern syntaxes (space-separated rgb(), 8-digit hex, color()) are NOT supported by these patterns, so a visually-valid color string can still fail.
Source
Thrown at rb/lib/selenium/webdriver/support/color.rb:71
new Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)
when RGB_PCT_PATTERN
array = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]
new(*array.map { |e| Float(e) / 100 * 255 })
when RGBA_PATTERN
new Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3), Regexp.last_match(4)
when RGBA_PCT_PATTERN
array = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]
new(*array.map { |e| Float(e) / 100 * 255 } << Regexp.last_match(4))
when HEX_PATTERN
array = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]
new(*array.map { |e| e.to_i(16) })
when HEX3_PATTERN
array = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]
new(*array.map { |e| (e * 2).to_i(16) })
when HSL_PATTERN, HSLA_PATTERN
from_hsl(Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3), Regexp.last_match(4))
else
raise ArgumentError, "could not convert #{str.inspect} into color"
end
end
def self.from_hsl(hue, sat, light, alpha)
hue = Float(hue) / 360
sat = Float(sat) / 100
light = Float(light) / 100
alpha = Float(alpha || 1)
if sat.zero?
r = light
g = r
b = r
else
luminocity2 = light < 0.5 ? light * (sat + 1) : light + sat - (light * sat)
luminocity1 = (light * 2) - luminocity2
r = hue_to_rgb(luminocity1, luminocity2, hue + (1.0 / 3.0))View on GitHub (pinned to aa36b38e69)
Solutions
- Normalize the string to a supported format (comma-separated rgb()/rgba()/hsl()/hsla() or #rrggbb) before calling from_string.
- If the value came from getCssValue, convert space-separated rgb(r g b / a) to comma form rgba(r, g, b, a).
- Map named colors to hex yourself before parsing, since from_string does not recognize color names.
Example fix
# before (browser may return space-separated form)
color = Selenium::WebDriver::Support::Color.from_string(element.css_value('background-color'))
# after
raw = element.css_value('background-color')
raw = raw.sub(/rgba?\(([^)]+)\)/) { |inner| "rgba(#{inner.tr(' /', ', ')})" }
color = Selenium::WebDriver::Support::Color.from_string(raw) Defensive patterns
Strategy: validation
Validate before calling
SUPPORTED_COLOR = /^\s*(rgb|rgba|hsl|hsla)\(|#\h{3}(?:\h{3})?\b/
def parseable_color?(str)
str.is_a?(String) && str.match?(SUPPORTED_COLOR)
end
color = parseable_color?(raw) ? Color.from_string(raw) : fallback Type guard
def legacy_css_color?(str)
return false unless str.is_a?(String)
patterns = [Color::RGB_PATTERN, Color::RGBA_PATTERN, Color::HEX_PATTERN,
Color::HEX3_PATTERN, Color::HSL_PATTERN, Color::HSLA_PATTERN]
patterns.any? { |p| str.match?(p) }
end Try / catch
begin
Color.from_string(raw)
rescue ArgumentError => e
raise unless e.message.include?('into color')
Color.from_string(normalize_to_legacy_rgb(raw))
end Prevention
- Normalize browser-returned css values (space-separated rgb) to comma form before parsing.
- Map named colors to hex yourself; from_string does not accept color names.
- Validate the string against a known pattern before calling from_string.
When it happens
Trigger: Calling Selenium::WebDriver::Support::Color.from_string with a named color ('red'), a modern css4 color function (color(srgb 1 0 0)), an 8-digit hex (#ff0000ff), a space-separated rgb (rgb(255 0 0)), a malformed/empty string, or a value with surrounding markup. Only the legacy comma-delimited rgb()/rgba()/hsl()/hsla() and #rgb/#rrggbb forms parse.
Common situations: Reading element.style.color or getCssValue('color') and passing it through Color.from_string; browsers increasingly return the space-separated rgb() form for getCssValue, which does not match RGBA_PATTERN and fails; test data with named colors.
Related errors
- Did not know how to convert ${value} into color
- Safari does not support options that are not namespaced
- unexpected tag name #{tag_name.inspect}
- Could not convert {str_} into color
- name is required
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/ef856c891309e60a.
Report an issue: GitHub.