SeleniumHQ/selenium · error · Error::NoSuchElementError
cannot locate option with value: #{value.inspect}
Error message
cannot locate option with value: #{value.inspect} What it means
In private select_by_value, if no <option> has an @value attribute matching the argument, the binding raises Error::NoSuchElementError. This is the value-attribute equivalent of 491 (text). Note it matches the value attribute, not visible text.
Source
Thrown at rb/lib/selenium/webdriver/support/select.rb:184
return select_options(opts) unless opts.empty?
raise Error::NoSuchElementError, "cannot locate element with text: #{text.inspect}"
end
def select_by_index(index)
opts = find_by_index index
return select_option(opts.first) unless opts.empty?
raise Error::NoSuchElementError, "cannot locate element with index: #{index.inspect}"
end
def select_by_value(value)
opts = find_by_value value
return select_options(opts) unless opts.empty?
raise Error::NoSuchElementError, "cannot locate option with value: #{value.inspect}"
end
def deselect_by_text(text)
raise Error::UnsupportedOperationError, 'you may only deselect option of a multi-select' unless multiple?
opts = find_by_text text
return deselect_options(opts) unless opts.empty?
raise Error::NoSuchElementError, "cannot locate element with text: #{text.inspect}"
end
def deselect_by_value(value)
raise Error::UnsupportedOperationError, 'you may only deselect option of a multi-select' unless multiple?
opts = find_by_value value
return deselect_options(opts) unless opts.empty?View on GitHub (pinned to aa36b38e69)
Solutions
- Inspect select.options.map { |o| o.attribute(:value) } to see the real value attributes.
- If visible text is more stable, use select_by(:text) instead.
- Add an explicit wait for the desired option's presence before selecting.
Example fix
# before
select.select_by(:value, 'foo')
# after
Selenium::WebDriver::Wait.new.until { select.options.any? { |o| o.attribute(:value) == 'foo' } }
select.select_by(:value, 'foo') Defensive patterns
Strategy: validation
Validate before calling
Selenium::WebDriver::Wait.new.until { select.options.any? { |o| o.attribute(:value) == value } }
select.select_by(:value, value) Type guard
def option_with_value?(select, value)
select.options.any? { |o| o.attribute(:value) == value }
end Try / catch
begin
select.select_by(:value, value)
rescue Selenium::WebDriver::Error::NoSuchElementError => e
raise unless e.message.include?('cannot locate option with value')
# value not present — wait/retry or assert
end Prevention
- Inspect select.options.map { |o| o.attribute(:value) } for real values.
- Wait for the option to be present before selecting.
- Use select_by(:text) when the value attribute is generated/unstable.
When it happens
Trigger: Calling select.select_by(:value, 'foo') when no <option value="foo"> exists. Common when the value differs from visible text, or the value is dynamically generated.
Common situations: Typo in value; value is a generated/id-like token that changed; option has no value attribute (empty) and the test passed a non-empty string; race where options aren't rendered yet.
Related errors
- no options are selected
- cannot locate element with text: #{text.inspect}
- cannot locate element with index: #{index.inspect}
- cannot locate option with index: #{index}
- unexpected tag name #{tag_name.inspect}
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/29ff496b17841e54.
Report an issue: GitHub.