SeleniumHQ/selenium · error · Error::NoSuchElementError
cannot locate element with text: #{text.inspect}
Error message
cannot locate element with text: #{text.inspect} What it means
In the private select_by_text helper, if no <option> whose visible text equals the argument is found, the binding raises Error::NoSuchElementError. This surfaces a genuine page-state mismatch (the option text you asked for is not present) using WebDriver's standard not-found error type.
Source
Thrown at rb/lib/selenium/webdriver/support/select.rb:168
# Deselect all selected options. Only valid if the element supports multiple selections.
#
# @raise [Error::UnsupportedOperationError] if the element does not support multiple selections.
#
def deselect_all
raise Error::UnsupportedOperationError, 'you may only deselect all options of a multi-select' unless multiple?
options.each { |e| deselect_option e }
end
private
def select_by_text(text)
opts = find_by_text text
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
View on GitHub (pinned to aa36b38e69)
Solutions
- Inspect select.options.map(&:text) to confirm the exact visible text available.
- Use select_by(:value, ...) matching the option's value attribute if the visible text is unstable.
- Wait for the option to be present (explicit wait) before selecting, to avoid load races.
Example fix
# before
select.select_by(:text, 'Bar')
# after
Selenium::WebDriver::Wait.new.until { select.options.any? { |o| o.text == 'Bar' } }
select.select_by(:text, 'Bar') Defensive patterns
Strategy: validation
Validate before calling
Selenium::WebDriver::Wait.new.until { select.options.any? { |o| o.text == text } }
select.select_by(:text, text) Type guard
def option_with_text?(select, text)
select.options.any? { |o| o.text == text }
end Try / catch
begin
select.select_by(:text, text)
rescue Selenium::WebDriver::Error::NoSuchElementError => e
raise unless e.message.include?('cannot locate element with text')
# text not present — wait/retry or fail the test with context
end Prevention
- Inspect select.options.map(&:text) to confirm exact visible text.
- Use an explicit wait for the option before selecting.
- Prefer select_by(:value) when visible text is volatile.
When it happens
Trigger: Calling select.select_by(:text, 'Bar') where no <option> has visible text exactly equal to 'Bar'. Whitespace-only or partial matches via find_by_text's fallback may still fail if no candidate matches exactly.
Common situations: Typo in the option text; the option text changed in a newer build; text includes extra whitespace the test didn't account for; the select is still loading options (race condition).
Related errors
- no options are selected
- cannot locate element with index: #{index.inspect}
- cannot locate option with value: #{value.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/c9d1c19dcd8906f8.
Report an issue: GitHub.