{"record":{"id":"e16048e566fbb023","repo":"SeleniumHQ/selenium","slug":"cannot-find-element-by-how-inspect","errorCode":null,"errorMessage":"cannot find element by #{how.inspect}","messagePattern":"cannot find element by #(.+?)","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"rb/lib/selenium/webdriver/common/search_context.rb","lineNumber":69,"sourceCode":"      # the entire document, not just the children of this current node. Use\n      # \".//\" to limit your search to the children of the receiving Element.\n      #\n      # @overload find_element(how, what)\n      #   @param [Symbol, String] how The method to find the element by\n      #   @param [String] what The locator to use\n      # @overload find_element(opts)\n      #   @param [Hash] opts Find options\n      #   @option opts [Symbol] :how Key named after the method to find the element by, containing the locator\n      # @return [Element]\n      #\n      # @raise [Error::NoSuchElementError] if the element doesn't exist\n      #\n\n      def find_element(*args)\n        how, what = extract_args(args)\n\n        by = SearchContext.finders[how.to_sym]\n        raise ArgumentError, \"cannot find element by #{how.inspect}\" unless by\n\n        bridge.find_element_by by, what, ref\n      end\n\n      #\n      # Find all elements matching the given arguments\n      #\n      # @see SearchContext#find_element\n      #\n\n      def find_elements(*args)\n        how, what = extract_args(args)\n\n        by = SearchContext.finders[how.to_sym]\n        raise ArgumentError, \"cannot find elements by #{how.inspect}\" unless by\n\n        bridge.find_elements_by by, what, ref\n      end","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/rb/lib/selenium/webdriver/common/search_context.rb#L51-L87","documentation":"Raised by SearchContext#find_element when the 'how' (locator strategy key) is not found in the FINDERS registry. The valid strategies are: :class, :class_name, :css, :id, :link, :link_text, :name, :partial_link_text, :relative, :tag_name, :xpath (plus any registered via SearchContext.extra_finders). Passing a typo, a string that doesn't match a registered symbol, or an unsupported strategy raises ArgumentError.","triggerScenarios":"Calling find_element(:class, 'btn') works, but find_element(:classname, 'btn') (no underscore) fails because :classname is not registered. Passing find_element(:tag, 'div') instead of :tag_name. Passing a plain string like find_element('css selector', 'div') where 'css selector' is not a key (use :css instead). Using a custom finder that was never registered via extra_finders.","commonSituations":"Copy-pasting CSS selector strategy strings from the W3C protocol spec ('css selector') instead of using the Ruby symbol (:css). Typos in locator strategy symbols. Code written against a different Selenium language binding (e.g., Python uses By.CSS_SELECTOR) ported incorrectly to Ruby.","solutions":["Use one of the registered symbols: :class, :class_name, :css, :id, :link, :link_text, :name, :partial_link_text, :relative, :tag_name, :xpath.","For CSS selectors use :css (not :css_selector or the string 'css selector').","For class name use :class or :class_name (not :classname).","For tag name use :tag_name (not :tag).","If you need a custom strategy, register it via Selenium::WebDriver::SearchContext.extra_finders = { my_strategy: 'my strategy' } before using it."],"exampleFix":"// before\nelement = driver.find_element(:classname, 'submit-btn')\nelement = driver.find_element('css selector', '#login')\n\n// after\nelement = driver.find_element(:class, 'submit-btn')\nelement = driver.find_element(:css, '#login')","handlingStrategy":"type-guard","validationCode":"VALID_FINDERS = %i[\n  class class_name css id link link_text name\n  partial_link_text relative tag_name xpath\n].freeze\n\ndef safe_find_element(driver, how, what)\n  raise ArgumentError, \"Unsupported finder: #{how.inspect}\" unless VALID_FINDERS.include?(how.to_sym)\n  driver.find_element(how, what)\nend","typeGuard":"def valid_locator_strategy?(how)\n  Selenium::WebDriver::SearchContext.finders.key?(how.to_sym)\nend","tryCatchPattern":"begin\n  driver.find_element(how, what)\nrescue ArgumentError => e\n  raise unless e.message.include?('cannot find element by')\n  # fall back to a default strategy or re-raise with context\n  raise ArgumentError, \"Invalid locator strategy '#{how}'. Valid: #{Selenium::WebDriver::SearchContext.finders.keys}\"\nend","preventionTips":["Centralize locator strategy constants in your test framework instead of inlining symbols.","Use the Selenium::WebDriver::SearchContext.finders.keys list to validate dynamically-supplied strategies.","Avoid passing raw strings from external config as strategies; always .to_sym and validate."],"tags":["find-element","locator-strategy","argument-error","ruby"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}