目录
一、查找1个元素
def findBy(self, by=By.ID, value=None):
try:
return self.driver.find_element_by_*(by, value)
except:
self.exception_handle2()
return self.driver.find_element(by, value)
def find(self, locate) -> WebElement:
return self.findBy(*locate)
具体使用:
_selected_icon2 = (MobileBy.ACCESSIBILITY_ID, 'option_selected_icon')
self.find(self._selected_icon2).click()
说明:如果_selected_icon2对应多个元素,这里只会取第一个
二、找到多个元素
def findAllBy(self, by=By.ID, value=None):
try:
return self.driver.find_elements_by_*(by, value)
except:
self.exception_handle2()
return self.driver.find_elements(by, value)
def findAll(self, locate) -> []:
return self.findAllBy(*locate)
具体使用:
_selected_icon2 = (MobileBy.ACCESSIBILITY_ID, 'option_selected_icon')
self.findAll(self._selected_icon2)[1].click()
三、目前支持哪些定位方式?
1、支持移动端常用定位方式
id
xpath
class name
accessibility id
2、不常用的也支持(但还没用过),以下为支持的定位方式
class By(object):
"""
Set of supported locator strategies.
"""
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
class MobileBy(By):
IOS_PREDICATE = '-ios predicate string'
IOS_UIAUTOMATION = '-ios uiautomation'
IOS_CLASS_CHAIN = '-ios class chain'
ANDROID_UIAUTOMATOR = '-android uiautomator'
ANDROID_VIEWTAG = '-android viewtag'
ANDROID_DATA_MATCHER = '-android datamatcher'
ACCESSIBILITY_ID = 'accessibility id'
IMAGE = '-image'
CUSTOM = '-custom'
四、find_element、find_elements从哪来的?
/appium/webdriver/webelement.py
-> find_element 部分
def find_element(self, by=By.ID, value=None):
"""Find an element given a By strategy and locator
Prefer the find_element_by_* methods when possible.
Args:
by (:obj:`str`, optional): The strategy
value (:obj:`str`, optional): The locator
Usage:
element = element.find_element(By.ID, 'foo')
Returns:
`appium.webdriver.webelement.WebElement`
"""
# TODO: If we need, we should enable below converter for Web context
# if self._w3c:
# if by == By.ID:
# by = By.CSS_SELECTOR
# value = '[id="%s"]' % value
# elif by == By.TAG_NAME:
# by = By.CSS_SELECTOR
# elif by == By.CLASS_NAME:
# by = By.CSS_SELECTOR
# value = ".%s" % value
# elif by == By.NAME:
# by = By.CSS_SELECTOR
# value = '[name="%s"]' % value
return self._execute(RemoteCommand.FIND_CHILD_ELEMENT,
{"using": by, "value": value})['value']
-> find_elements 部分
def find_elements(self, by=By.ID, value=None):
"""Find elements given a By strategy and locator
Prefer the find_elements_by_* methods when possible.
Args:
by (:obj:`str`, optional): The strategy
value (:obj:`str`, optional): The locator
Usage:
element = element.find_elements(By.CLASS_NAME, 'foo')
Returns:
:obj:`list` of :obj:`appium.webdriver.webelement.WebElement`
"""
# TODO: If we need, we should enable below converter for Web context
# if self._w3c:
# if by == By.ID:
# by = By.CSS_SELECTOR
# value = '[id="%s"]' % value
# elif by == By.TAG_NAME:
# by = By.CSS_SELECTOR
# elif by == By.CLASS_NAME:
# by = By.CSS_SELECTOR
# value = ".%s" % value
# elif by == By.NAME:
# by = By.CSS_SELECTOR
# value = '[name="%s"]' % value
return self._execute(RemoteCommand.FIND_CHILD_ELEMENTS,
{"using": by, "value": value})['value']