测试环境准备
1.PC端安装chrome,手机端安装谷歌浏览器(部分品牌应用商店可能会没有,可以安装应用宝,再到应用宝上安装google浏览器,或者PC端应用宝内下载。不太清楚这个有什么用途,我不按安装也是可以运行的。)
2.打开手机的webview调试模式
3.浏览器输入 chrome://inspect/devices#devices,可以看到当前webview页面,如果获取不到可以刷新下,或者调试模式未打开
配置:预期功能 Desired Capabilities
{
"platformName":
"Android",
"platformVersion":
"10",
"deviceName": "红米note7",
"udid":
"xxxxx",
"clearSystemFiles":
true,
"newCommandTimeout":
"360",
"automationName":
"uiautomator2",
"resetKeyboard":
true,
"unicodeKeyboard":
true,
"skipServerInstallation":
false,
"skipDeviceInitialization":
false,
"noReset": true,
"browserName":
"Chrome",
"unlockType":
"pattern",
"unlockKey":
""
}
import time from appium import webdriver from appium.webdriver.mobilecommand import MobileCommand
desired_caps_a = {‘platformName‘: "Android", ‘platFormVersion‘: "10", ‘deviceName‘: "红米note7", ‘udid‘: "xxxxxx", ‘automationName‘: "UiAutomator2", ‘noReset‘: True, ‘unicodeKeyboard‘: True, ‘resetKeyboard‘: True, ‘skipServerInstallation‘: True, ‘skipDeviceInitialization‘: True, ‘newCommandTimeout‘: ‘360‘, ‘browserName‘: ‘Chrome‘ } device_a = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps_a) time.sleep(2) # 测试获取H5元素 device_a.get("http://url") # 要浏览器访问的url地址 print(device_a.contexts) # 打印出来的是[‘NATIVE_APP‘, ‘CHROMIUM‘] device_a.implicitly_wait(30) device_a.switch_to.context(‘CHROMIUM‘) # H5元素操作,切换到H5页面定位操作 device_a.find_element_by_xpath(".//div[text()=‘请假‘]").click() device_a.find_element_by_xpath(".//div[text()=‘假别‘]").click() device_a.switch_to.context(‘NATIVE_APP‘) # android原生控件操作,切换回app操作 device_a.get_window_size() device_a.swipe(int(0.5*1080), int(0.95*2217), int(0.5*1080), int(0.93*2217), 1000) device_a.find_element_by_xpath(".//android.view.View[@text=‘确定‘]").click()