元素操作
| |
| driver.find_element_by_id("com.wuba.zhuanzhuan:id/ae8").click() |
| |
| driver.find_element_by_id("com.wuba.zhuanzhuan:id/ij").clear() |
| |
| driver.find_element_by_id("com.wuba.zhuanzhuan:id/ij").send_keys("test content") |
| |
| print(driver.find_element_by_xpath(" //android.widget.LinearLayout[1]//xxx").text) |
- size 获取元素大小
- get_attribute 获取元素属性
- set_value 设置值
- location 获取元素位置
- is_selected() 元素是否被选中
- is_enabled() 元素是否被启用
- is_displayed() 元素是否显示
触摸操作
前提需要先是实例化
| from appium.webdriver.common.touch_action import TouchAction |
| touch = TouchAction(driver) |
| |
| ele = driver.find_element(AppiumBy.ID, "com.zhao.myreader:id/iv_search") |
| touch.tap(ele) |
| touch.perform() |
| |
| ele = driver.find_element(AppiumBy.ID, "com.zhao.myreader:id/iv_search") |
| touch.press(ele) |
| touch.perform() |
| |
| ele = driver.find_element(AppiumBy.ID, "com.zhao.myreader:id/iv_search") |
| touch.press(ele).release() |
| touch.perform() |
| |
| touch.press(x=55, y=1100) |
| touch.wait(ms=100) |
| touch.move_to(x=900, y=1100) |
| touch.release() |
| |
| ele2 = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "读书屋") |
| touch.long_press(ele2) |
| |
| def getSize(driver): |
| x = driver.get_window_size()['width'] |
| y = driver.get_window_size()['height'] |
| return (x, y) |
| |
| def swipeUp(driver,t=1000): |
| l = getSize(driver) |
| x1 = int(l[0] * 0.5) |
| y1 = int(l[1] * 0.75) |
| y2 = int(l[1] * 0.25) |
| driver.swipe(x1, y1, x1, y2,t) |
| |
| def swipeDown(driver,t=1000): |
| l = getSize(driver) |
| x1 = int(l[0] * 0.5) |
| y1 = int(l[1] * 0.25) |
| y2 = int(l[1] * 0.75) |
| driver.swipe(x1, y1, x1, y2,t) |
| |
| def swipLeft(driver,t): |
| l=getSize(driver) |
| x1=int(l[0]*0.75) |
| y1=int(l[1]*0.5) |
| x2=int(l[0]*0.05) |
| driver.swipe(x1,y1,x2,y1,t) |
| |
| def swipRight(driver,t=1000): |
| l=getSize(driver) |
| x1=int(l[0]*0.05) |
| y1=int(l[1]*0.5) |
| x2=int(l[0]*0.75) |
| driver.swipe(x1,y1,x2,y1,t) |
多指操作
使用单指操作TouchAction 对象实例对象,然后将单指的对象添加到多指的对象里面即可,注意单指操作里面不要使用执行命令
| from appium.webdriver.common.multi_action import MultiAction |
| action1 = TouchAction(driver) |
| action2 = TouchAction(driver) |
| window_size = driver.get_window_size() |
| width = window_size["width"] |
| height = window_size["height"] |
| action1 = TouchAction(driver) |
| action2 = TouchAction(driver) |
| |
| action1.press(x=width / 3, y=height / 3, pressure=2).wait(100).move_to(x=width / width, y=height / height).release() |
| action2.press(x=width / 2, y=height / 2, pressure=2).wait(100).move_to(x=width / 1, y=height / 1).release() |
| multi_action = MultiAction(driver) |
| multi_action.add(action1) |
| multi_action.add(action2) |
| multi_action.perform() |
系统事件
| press_keycode(AndroidKeyCode)#发送按键事件 |
| # 例如:点击home键,home键的KeyCode是3 |
| driver.press_keycode(3) |
KEYCODE_CALL 拨号键 5
| KEYCODE_ENDCALL 挂机键 6 |
| KEYCODE_HOME 按键Home 3 |
| KEYCODE_MENU 菜单键 82 |
| KEYCODE_BACK 返回键 4 |
| KEYCODE_SEARCH 搜索键 84 |
| KEYCODE_CAMERA 拍照键 27 |
| KEYCODE_FOCUS 拍照对焦键 80 |
| KEYCODE_POWER 电源键 26 |
| KEYCODE_NOTIFICATION 通知键 83 |
| KEYCODE_MUTE 话筒静音键 91 |
| KEYCODE_VOLUME_MUTE 扬声器静音键 164 |
| KEYCODE_VOLUME_UP 音量增加键 24 |
| KEYCODE_VOLUME_DOWN 音量减小键 25 |
driver的一些重要操作
- start_activity(包名,activity名)
| |
| driver.start_activity("com.wuba.zhuanzhuan","./presentation.view.activity.LaunchActivity") |
| |
| driver.contexts |
| |
| ['NATIVE_APP', 'WEBVIEW_com.android.browser'] |
| |
| NATIVE_APP:native的context |
| WEBVIEW_com.android.browser:webview的context,存放html的容器 |
- switch_to.context(context名)
| #切换context |
| driver.switch_to.context("WEBVIEW_com.wuba.zhuanzhuan") |
NATIVE时不能定位WEBVIEW的内容,在WEBVIEW的context时不能定位NATIVE的内容。
所以需要切换到对应的context中去进行操作
- scroll(起始元素,结束元素)
driver.scroll(origin_el,destination_el)
- page_source # 获得当前页面的所有元素
driver.page_source # 这可以用来判断元素是否存在,例如(assert “发布成功” in driver.page_source)
- 补充一些driver启动时可能用到的项
| autoLaunch :Appium是否要自动启动或安装app,默认true |
| desired_caps[‘autoLaunch’] = ‘false’ |
| 有的时候我不想让appium每次都启动app,想自己去启动activity,那这个项这时就可以起作用了 |
| |
| noReset:在会话前是否重置app状态。默认是false |
| desired_caps[‘noReset’] = ‘true’ |
| |
| newCommandTimeout:设置未接收到新命令的超时时间,默认60s |
| 如果60s内没有接收到新命令,appium会自动断开连接,如果我需要很长时间做driver之外的操作,可能延长接收新命令的超时时间 |
| desired_caps[“newCommandTimeout”]=1800 |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?