Appium 基础
安装 APK
import time
from appium.webdriver import Remote
# apk 的路径
file = r'apk路径'
caps = {"platformName": "Android",
"deviceName": "设备号", # 可通过 adb devices 或者 adb get-serialno 获取
"app": file}
driver=Remote(command_executor='http://127.0.0.1:4723/wd/hub',desired_capabilities=caps) # 4723要与Appium客户端的端口号对应,4723为默认值
time.sleep(5)
driver.quit()
启动 APK
'''
获取apk包名
adb shell dumpsys activity activities | find "mFocusedActivity"
aapt dumpsys badging 电脑端apk路径 通过应用查看包名(配置aapt环境变量,或在aapt.exe路径下运行)
adb logcat | findStr -i displayed
adb shell pm list package -s 查询系统apk包名
adb shell pm list package -3 查询第三方apk包名
adb shell am monitor 查看将要启动或退出app的包名
adb shell dumpsys window windows | findstr mFocusedApp 查看当前界面的app的包名
adb shell dumpsys activity top | find "ACTIVITY" 查看启动的app的包名
adb shell dumpsys activity activities | findstr "Run" 查看所有启动的应用的包名
adb shell dumpsys window w |findstr \/ |findstr name= 查看当前启动应用的包名
'''
import time
from appium.webdriver import Remote
caps = {"platformName": "Android",
"deviceName": "设备号",
"appPackage": "包名",
"appActivity": ".guide.SplashActivity",
"noReset": True} # 不要停止应用程序,不要清除应用数据
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub', desired_capabilities=caps)
time.sleep(2)
driver.quit()
查找元素
import time
from appium.webdriver import Remote
caps = {"platformName": "Android",
"deviceName": "设备号",
"appPackage": "包名",
"appActivity": ".guide.SplashActivity",
"noReset": True}
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub', desired_capabilities=caps)
# 隐性等待
driver.implicitly_wait(10)
# 找元素
# xpath定位://class[@属性="值"]
xpath = "//android.widget.ImageView[@resource-id='resource-id值' and @index='1']"
el = driver.find_element("xpath", xpath)
# el = driver.find_element("class name","android.widget.ImageView")
print(el)
print(el.text) # 打印文本
print(el.rect) # 打印该元素的坐标点以及长宽
print(el.get_attribute('class'))
el.click()
time.sleep(3)
driver.quit()
显性等待
import time
from appium.webdriver import Remote
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
caps = {"platformName": "Android",
"deviceName": "设备号",
"appPackage": "包名",
"appActivity": ".activity.MainActivity",
"noReset": True}
driver = Remote(command_executor="http://127.0.0.1:4723/wd/hub", desired_capabilities=caps)
xpath = '//android.widget.ImageView[@resource-id="com.qiduo.mail:id/titlebar_toggle_slidingmenu_btn"]'
# 显性等待
locator = ("xpath", xpath)
wait = WebDriverWait(driver, timeout=8)
wait.until(expected_conditions.presence_of_element_located(locator))
el = driver.find_element("xpath", xpath)
print(el.text)
print(el.rect)
el.click()
time.sleep(3)
driver.quit()
打开 APP 指定到的活动页面
import time
from appium.webdriver import Remote
caps = {"platformName": "Android",
"deviceName": "设备号",
"appPackage": "包名",
"appActivity": ".activity2.MainActivity",
"noReset": True}
driver = Remote(command_executor="http://127.0.0.1:4723/wd/hub", desired_capabilities=caps)
driver.implicitly_wait(10)
# driver.start_activity("包名", "活动页面名称")
driver.start_activity("包名", ".activity2.LoginActivity") # 打开app指定到的活动页面
# send_keys
driver.find_element("id","定位元素1").send_keys("123456") # 账号
driver.find_element("id","定位元素2").send_keys("abcdef") #密码
driver.find_element("id","定位元素3").click() # 登录
time.sleep(3)
driver.quit()
滑动页面
import time
from appium.webdriver import Remote
caps = {"platformName": "Android",
"deviceName": "设备号",
"appPackage": "包名",
"appActivity": ".activity2.MainActivity"}
driver = Remote(command_executor="http://127.0.0.1:4723/wd/hub", desired_capabilities=caps)
time.sleep(2)
# 获取屏幕尺寸
size = driver.get_window_size()
print(size)
width = size["width"]
height = size["height"]
# 从右往左滑动 driver.swipe(起始点x坐标, 起始点y坐标, 结束点x坐标, 结束点y坐标, 滑动的时间间隔(可以省略))
driver.swipe(width * 0.9, height * 0.5, width * 0.1, height * 0.5, 1000)
driver.implicitly_wait(10)
driver.find_element("id", "定位元素1").click()
driver.find_element("id", "定位元素2").click()
time.sleep(3)
driver.quit()
按键操作
import time
from appium.webdriver import Remote
class Key(object):
"""对按键进行封装"""
enter = 66
home = 3
volume_up = 24
volume_down = 25
def press_enter(driver):
"""对 enter 键进行单独的封装"""
driver.press_keycode(Key.enter)
def press_home(driver):
"""对 home 键进行单独的封装"""
driver.press_keycode(Key.home)
caps = {"platformName": "Android",
"deviceName": "设备号",
"appPackage": "包名",
"appActivity": ".activity2.MainActivity",
"noReset": True}
driver = Remote(command_executor="http://127.0.0.1:4723/wd/hub", desired_capabilities=caps)
# 音量+
time.sleep(3)
driver.press_keycode(Key.volume_up) # 音量+键
time.sleep(2)
# 按下home进一步的封装
press_home(driver)
time.sleep(2)
driver.quit()
手机 toast 弹窗处理
import time
from appium.webdriver import Remote
caps = {"platformName": "Android",
"deviceName": "设备号",
"appPackage": "包名",
"appActivity": ".activity2.MainActivity",
"automationName": "UiAutomator2",
"noReset": True}
driver = Remote(command_executor="http://127.0.0.1:4723/wd/hub", desired_capabilities=caps)
driver.implicitly_wait(10)
time.sleep(3)
driver.start_activity("包名", ".activity2.LoginActivity")
driver.find_element("id", "元素").click() # 登录
# 定位 toast
toast = driver.find_element("xpath", "//android.widget.Toast") # 固定语法
# toast = driver.find_element("xpath", "//*[contains(@text, '用户名和密码不能为空')]") # 通过toast文本定位
print(toast.text)
time.sleep(3)
driver.quit()
webview 处理(H5页面)
-
class 中,存在
android.webkit.WebView
需要用 webview 来处理 -
谷歌浏览器访问
chrome://inspect
确认手机 webview 版本,下载chromedriver.exe
驱动 -
webview 中的元素可以通过
UC 开发者工具定位
,在设置
中选择本地 Devtools Inspector UI 资源
,在'Home'中点击inspect
即可查看
import time
from appium.webdriver import Remote
caps = {"platformName": "Android",
"chromedriverExecutableDir": r"D:\code\appium_record", # 驱动位置
"deviceName": "设备号",
"appPackage": "包名",
"appActivity": ".activity.MainActivity"}
driver = Remote(command_executor="http://127.0.0.1:4723/wd/hub", desired_capabilities=caps)
driver.implicitly_wait(10)
# 点击 师资团队 进入web页面
driver.find_element("xpath", "//*[@text='text的文本信息']").click()
time.sleep(5)
# 打印所有的上下文
print(driver.contexts)
# 上下文切换
driver.switch_to.context(driver.contexts[-1])
# driver.switch_to.context('WEBVIEW的名称')
# 注意:有时候一个页面会包含多个标签
print(driver.window_handles)
# 点击查看全部
driver.find_element("xpath", "//*[@id='__impage-component-wrapper-1']/section/div[1]/div/span/span").click()
time.sleep(3)
driver.quit()
标签:包名,Remote,App,自动化,driver,caps,time,import
From: https://www.cnblogs.com/HollowPan/p/17629956.html