python+appium(2)

一、app常用操作

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy as by
# from selenium.webdriver.support.wait import WebDriverWait
# from selenium.webdriver.support import expected_conditions as ec
from appium.webdriver.extensions.android.nativekey import AndroidKey
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.multi_action import MultiAction

url = 'http://127.0.0.1:4732/wd/hub'
caps = {
    
    }
caps['paltfromName'] = 'Android'

driver = webdriver.Remote(url, caps)

# 截图
driver.save_screenshot('存放路径/图片名称.png')

# 滑动
driver.swipe(130, 1300, 130, 1400)  # 起始点130,1300;结束点130,1400

# 锁屏
driver.lock()

# 获取手机状态,是否锁屏
print(driver.is_locked())

# 唤醒手机,尽量不设置密码或人脸识别
driver.unlock()
driver.swipe(500, 3000, 500, 800)  # 向上滑动解锁

# 键盘事件输入解锁密码,密码是1122
driver.press_keycode(AndroidKey.DIGIT_1)
driver.press_keycode(AndroidKey.DIGIT_1)
driver.press_keycode(AndroidKey.DIGIT_2)
driver.press_keycode(AndroidKey.DIGIT_2)

# 获取源码
print(driver.page_source)

# 点击坐标轴,x=130;y=1300
driver.tap([(130, 1300)])

# 手机返回按钮
driver.back()

# 长按home键
driver.long_press_keycode(AndroidKey.HOME)

二、元素常用操作

ele = driver.find_element(by.ACCESSIBILITY_ID, '菜单')

# 元素截图
ele.screenshot('存放路径/图片名称.png')

# 常用的元素属性判断
print(ele.is_enabled())  # 判断元素是否已经启用,启用后才能操作,比如点击、输入
print(ele.is_selected())  # 判断元素是否被选择用,几乎不使用,appium是继承selenium的,里面有这个操作,但是selenium是做web自动化的
print(ele.is_displayed())  # 判断元素是否已经显示

# 获取元素的属性,底层都是这一个方法
print(ele.get_attribute('text'))
print(ele.get_attribute('calss'))
print(ele.get_attribute('insex'))
print(ele.get_attribute('displayed'))
print(ele.get_attribute('content-desc'))

# 获取元素位置属性,x和y
print(ele.location)

# 获取元素尺寸属性,height和width
print(ele.size)
# 元素中心点计算公式 x + width/2 , y + height/2

# 长按元素
action = TouchAction(driver)
action.long_press(ele)  # 可以接收元素对象或坐标
action.perform()  # 执行语句

# 长按并移动
action = TouchAction(driver)

element = driver.find_elements(by.ID, '图书列表的id')

e1_location = element[0].location
e1_size = element[0].size
e1_x = e1_location['x'] + e1_size['width'] / 2
e1_y = e1_location['y'] + e1_size['height'] / 2

e2_location = element[1].location
e2_size = element[1].size
e2_x = e2_location['x'] + e2_size['width'] / 2
e2_y = e2_location['y'] + e2_size['height'] / 2

# driver.drag_and_drop(element[0], element[1]);这个方法使用失败
action.long_press(e1_x, e1_y)
action.move_to(e2_x, e2_y)
action.release()  # 释放,即松手
action.perform()

三、多指操作

# 多指操作,多指操作不需要单指的执行action.perform()
driver.tap([(900, 1700)])  # 手机图库位置
driver.tap([(500, 900)])  # 对应图片位置
driver.tap([(108, 360)])  # 打开图片

action_1 = TouchAction(driver)
action_1.long_press(x=630, y=1100)
action_1.move_to(x=900, y=700)
action_1.release()

action_2 = TouchAction(driver)
action_2.long_press(x=300, y=1400)
action_2.move_to(x=200, y=1900)
action_2.release()

mu = MultiAction(driver)
mu.add(action_1)
mu.add(action_2)
mu.perform()  # 由多指对象执行命令

四、获取页面列表元素并存入自定义列表

list_1 = []
list_1.extend([i.text for i in WebDriverWait.until(ec.visibility_of_all_elements_located((by.ID, '元素id')))])

while True:
    f1 = driver.page_source  # 源码
    """滑动的操作"""
    # 滑动方式一
    driver.swipe(start_x=500, start_y=1700, end_x=500, end_y=600)

    # 滑动方式二
    action = TouchAction(driver)
    action.press(x=500, y=1700)
    action.move_to(x=500, y=600)
    action.release()
    action.perform()

    # 滑动方式三
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.actions import interaction
    from selenium.webdriver.common.actions.action_builder import ActionBuilder
    from selenium.webdriver.common.actions.pointer_actions import PointerInput

    actions = ActionChains(driver)
    '''
    POINTER_MOUSE = "mouse",鼠标事件
    POINTER_TOUCH = "touch",触控事件
    POINTER_PEN = "pen",触控笔事件
    '''
    actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, 'touch'))
    actions.w3c_actions.pointer_action.move_to_location(x=500, y=1700)  # 起始点击
    actions.w3c_actions.pointer_action.pointer_down()  # 按下
    actions.w3c_actions.pointer_action.move_to_location(x=500, y=600)  # 终止位置
    actions.w3c_actions.pointer_action.release()  # 释放
    actions.perform()

    """判断去重 和 添加列表的操作"""
    elements = WebDriverWait.until(ec.visibility_of_all_elements_located((by.ID, '元素id')))
    elements_text = [i.text for i in elements]
    # for i in elements_text:
    #     if i not in list_1:  # 判断 list_1 中不存在 i ,则添加到 list_1 中
    #         list_1.append(i)
    [list_1.append(i) for i in elements_text if i not in list_1]  # 同上已注释代码效果一致

    """判断 f1 是否和 driver.page_source 一致,一致则退出循环"""
    if f1 == driver.page_source:
        break

print(list_1[::-1])  # 倒序查看列表

猜你喜欢

转载自blog.csdn.net/qq_36562656/article/details/128356559