appium 元素操作

find_element_by_id()  通过id来定位

find_element_by_name()  通过text定位,不稳定 已废除。可以修改文件恢复

find_element_by_classname()  通过classname来定位

find_element_by_xpath()  通过xpath来定位

list定位:find_elements_by_xx()  查找多组元素  索引确定

相对定位:无id、class重复的情况下。通过找父元素唯一后,再找子元素

element = driver.find_element_by_id()   父元素  

element.find_element_by_classname()  子元素

 

Uiautomator元素定位:

id,text,classname

 

driver.find_element_by_android_uiautomator('new UiSelector().resourceId(" id ")')

driver.find_element_by_android_uiautomator('new UiSelector().text(" text ")')

driver.find_element_by_android_uiautomator('new UiSelector().className(" classname ")')

 

元素等待:

  1.强制等待:

    time.sleep();

  2.隐形等待:

    driver.implicity_wait();针对所有元素

  3.显示等待:  针对特定元素

    from selenium.webdriver.support.ui import webDriverwait

    webDriverWait(driver, timeout, poll_frequency=0.5,  ignore_exceptions=None)

    driver:Webdriver

    timeout:超时时间

    poll_frequency:休眠时间 间隔默认0.5秒

    ignore_exceptions:超时后的异常信息,默认情况下抛出Nosuchelementexception

    一般和until、until_not()方法配用

    用法:webDriverWait(driver, timeout, poll_frequency=0.5,  ignore_exceptions=None).until(lambda x:x.find_element_by_id())

 

滑动操作:屏幕滑动,朋友圈上下滑动、浏览图片左右滑动

  driver.swipe(x1,y1,x2,y2,time)

连续滑动:TouchAction

  from appium.webdriver.common.touch_action import TouchAction

  press(el = None, x, y) 按压:

  TouchAction(driver).press()

  

  长按:longPress(el, x, y, duration=1000) 多了一个按压时间

  点击:tap(el, x, y, count=1)  点击操作

  移动:move_to(el, x, y)将指针从上一个点移动到下一个

  暂停:Wait(ms=0) 暂停毫秒

  释放:release() 结束的行动  取消屏幕的指针

  perform():执行操作

 

MultiAction:多点移动,放大 缩小 地图

  from appium.webdriver.common.multi_action import MultiAction

  add(*touch_action) 加载 将touchaction对象添加到multiaction中。稍后执行

  perform() 执行

  用法:

    a1 = TouchAction(driver).press(el).move_to(e2).perform()

    a2 = TouchAction(driver).press(e3).move_to(e4).perform()

    MultiAction(driver).add(a1, a2).perform()

 

定位toast消息:

  1.安装下载 Uiautomator2:

    cmd命令:cnpm appium-uiautomator2-driver

  2.在capability参数配置中:

    desired_caps['automationName'] = "uiautomator2"

  3.定位:

    message = "xxxxx"

    toast_message = ".//*[@text=\'{}\']".format(message)

    toast-element = driver.find_element_by_xpath(toast_message)

posted @ 2018-11-20 23:36  红豆·  阅读(513)  评论(0编辑  收藏  举报