#15 楼 @enumerate robotframework-appiumlibrary 是这样封装的:
 def click_element(self, locator):
    """Click element identified by `locator`.
    Key attributes for arbitrary elements are `index` and `name`. See
    `introduction` for details about locating elements.
    """
    self._info("Clicking element '%s'." % locator)
    self._element_find(locator, True, True).click()
 
将 locator 参数(name=xxx,id=xxx)传入_element_find 方法定位元素
 def _element_find(self, locator, first_only, required, tag=None):
    application = self._current_application()
    elements = self._element_finder.find(application, locator, tag)
    if required and len(elements) == 0:
        raise ValueError("Element locator '" + locator + "' did not match any elements.")
    if first_only:
        if len(elements) == 0: return None
        return elements[0]
    return elements
 
_element_finder 是 class ElementFinder 的实例,有如下定位策略
 def __init__(self):
    self._strategies = {
        'identifier': self._find_by_identifier,
        'id': self._find_by_id,
        'name': self._find_by_name,
        'xpath': self._find_by_xpath,
        'class': self._find_by_class_name,
        'accessibility_id': self._find_element_by_accessibility_id,
        'android': self._find_by_android,
        'ios': self._find_by_ios,
        'css': self._find_by_css_selector,
        None: self._find_by_default
    }
 
通过_parse_locator(locator) 获取到定位策略和定位标准,调用 strategies 内定义的定位方法
 def find(self, browser, locator, tag=None):
    assert browser is not None
    assert locator is not None and len(locator) > 0
    (prefix, criteria) = self._parse_locator(locator)
    strategy = self._strategies.get(prefix)
    if strategy is None:
        raise ValueError("Element locator with prefix '" + prefix + "' is not supported")
    (tag, constraints) = self._get_tag_and_constraints(tag)
    return strategy(browser, criteria, tag, constraints)
 
_parse_locator(locator) 对传入的定位策略 id=xxx,name=xxx 进行解析,=号前面的是定位方法,后面的是定位标准。
 def _parse_locator(self, locator):
    prefix = None
    criteria = locator
    if not locator.startswith('//'):
        locator_parts = locator.partition('=')
        if len(locator_parts[1]) > 0:
            prefix = locator_parts[0].strip().lower()
            criteria = locator_parts[2].strip()
    return (prefix, criteria)