Class: Watir::ElementLocator

Inherits:
Object
  • Object
show all
Includes:
Exception
Defined in:
lib/watir-webdriver/locators/element_locator.rb

Constant Summary collapse

WD_FINDERS =
[
  :class,
  :class_name,
  :css,
  :id,
  :link,
  :link_text,
  :name,
  :partial_link_text,
  :tag_name,
  :xpath
]
WILDCARD_ATTRIBUTE =
/^(aria|data)_(.+)$/

Instance Method Summary collapse

Constructor Details

#initialize(wd, selector, valid_attributes) ⇒ ElementLocator

Returns a new instance of ElementLocator.



21
22
23
24
25
# File 'lib/watir-webdriver/locators/element_locator.rb', line 21

def initialize(wd, selector, valid_attributes)
  @wd               = wd
  @selector         = selector.dup
  @valid_attributes = valid_attributes
end

Instance Method Details

#locateObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/watir-webdriver/locators/element_locator.rb', line 27

def locate
  e = by_id and return e # short-circuit if :id is given

  if @selector.size == 1
    element = find_first_by_one
  else
    element = find_first_by_multiple
  end

  # This actually only applies when finding by xpath/css - browser.text_field(:xpath, "//input[@type='radio']")
  # We don't need to validate the element if we built the xpath ourselves.
  # It is also used to alter behavior of methods locating more than one type of element
  # (e.g. text_field locates both input and textarea)
  validate_element(element) if element
rescue Selenium::WebDriver::Error::NoSuchElementError, Selenium::WebDriver::Error::StaleElementReferenceError
  nil
end

#locate_allObject



45
46
47
48
49
50
51
# File 'lib/watir-webdriver/locators/element_locator.rb', line 45

def locate_all
  if @selector.size == 1
    find_all_by_one
  else
    find_all_by_multiple
  end
end