SeleniumHQ/selenium · error · ValueError
Could not convert {str_} into color
Error message
Could not convert {str_} into color What it means
Color.from_string tries the rgb/rgba/rgb-percent/rgba-percent/hex/hex3/hsl/hsla regexes and the named-color lookup; if none match it raises ValueError naming the offending string. Note the hex regexes are not anchored at end-of-string, so partial/odd hex inputs may behave unexpectedly.
Source
Thrown at py/selenium/webdriver/support/color.py:99
if m.match(RGB_PCT_PATTERN, str_):
rgb = tuple(float(each) / 100 * 255 for each in m.groups)
return cls(*rgb)
if m.match(RGBA_PATTERN, str_):
return cls(*m.groups)
if m.match(RGBA_PCT_PATTERN, str_):
rgba = tuple([float(each) / 100 * 255 for each in m.groups[:3]] + [m.groups[3]])
return cls(*rgba)
if m.match(HEX_PATTERN, str_):
rgb = tuple(int(each, 16) for each in m.groups)
return cls(*rgb)
if m.match(HEX3_PATTERN, str_):
rgb = tuple(int(each * 2, 16) for each in m.groups)
return cls(*rgb)
if m.match(HSL_PATTERN, str_) or m.match(HSLA_PATTERN, str_):
return cls._from_hsl(*m.groups)
if str_.upper() in Colors:
return Colors[str_.upper()]
raise ValueError(f"Could not convert {str_} into color")
@classmethod
def _from_hsl(cls, h: ParseableFloat, s: ParseableFloat, light: ParseableFloat, a: ParseableFloat = 1) -> Color:
h = float(h) / 360
s = float(s) / 100
_l = float(light) / 100
if s == 0:
r = _l
g = r
b = r
else:
luminocity2 = _l * (1 + s) if _l < 0.5 else _l + s - _l * s
luminocity1 = 2 * _l - luminocity2
def hue_to_rgb(lum1: float, lum2: float, hue: float) -> float:
if hue < 0.0:
hue += 1View on GitHub (pinned to aa36b38e69)
Solutions
- Trim and validate the string against a known CSS color format before parsing.
- Wrap the call in try/except and fall back to a default color.
- If the format is known, use the matching constructor directly.
Example fix
# before
c = Color.from_string(user_input) # may be 'reed'
# after
try:
c = Color.from_string(user_input.strip())
except ValueError:
c = Color.from_string("#000000") Defensive patterns
Strategy: try-catch
Validate before calling
import re
VALID_COLOR = re.compile(r'^(#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})|rgb\([^)]*\)|rgba\([^)]*\)|hsl\([^)]*\)|hsla\([^)]*\)|[A-Za-z]+)$')
def looks_like_color(s: str) -> bool:
return bool(VALID_COLOR.match(s.strip())) Type guard
def looks_like_color(s: str) -> bool:
import re
return bool(re.match(r'^(#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})|rgba?\([^)]*\)|hsla?\([^)]*\)|[A-Za-z]+)$', s.strip())) Try / catch
try:
c = Color.from_string(raw)
except ValueError:
c = Color.from_string(DEFAULT) Prevention
- Strip whitespace/quotes from css-derived color strings before parsing.
- Prefer hex or rgb() formats when you control the input.
When it happens
Trigger: Color.from_string('reed'), Color.from_string('123'), Color.from_string('#GGGGGG'), or strings with stray whitespace/units/quotes that defeat every pattern.
Common situations: Reading color from element.value_of_css_property('color'/'background-color') on browsers that return an unexpected format, or processing user-typed color strings.
Related errors
- Did not know how to convert ${value} into color
- could not convert #{str.inspect} into color
- Error executing command for ${smBinary} with ${args}: ${e.to
- input must be a string
- Invalid locator
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/559f8cfdfc2c1920.
Report an issue: GitHub.