06 APP 自动化- H5 元素定位
文章目录
- 1、APP 分类
- 2、H5 元素
- 3、H5 元素定位环境的搭建
- 4、代码实现:
1、APP 分类
- 1、Android 原生 APP
- 2、混合 APP(Android 原生控件+H5页面)
- 3、纯 H5 App
2、H5 元素
- H5元素:通过 android.system.WebView 实现展示网页上的元素
- 如何实现在 app 应用程序上展示的网页?
appium—》xxxdriver(浏览器驱动)----->android.system.WebView—>展示网页
3、H5 元素定位环境的搭建
-
1、手机下载 chrome 浏览器
-
2、pc 下载谷歌浏览器
-
3、下载 chrome driver (自己电脑上的谷歌浏览器版本必须与驱动的版本匹配)
-
4、查看 WebView 是否开启,前提 WebView 处于 debug 状态下,才可以进入谷歌浏览器的调试界面,才可以展示到webview:
- 调试地址:
chrome://inspect/#devices
- webview开启调试模式:
- app中配置如下代码:
在WebView类中调用静态方法
setWebContentsDebuggingEnabled(): if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.KITKAT) { WebView.setWebContentsDebuggingEnabled(true);
- 注意:一般需要 app 开发人员开启
- app中配置如下代码:
- 调试地址:
-
5、定位 H5 元素的工具:uc-devtools
- 设置页面-选择本地资源
- 首页-点击目标页面下面的 inspect
- 设置页面-选择本地资源
4、代码实现:
# -*- coding=utf-8 -*-
import time
from time import sleep
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec"""
h5 元素定位
"""
# 设置操作终端的配置参数
desired_caps = dict(platformName='Android', # 指定操作系统platformVersion='12',# 指定操作系统版本automationName='Uiautomator2',# 默认框架deviceName='127.0.0.1:62001',# 指定设备名称appPackage='com.wondershare.drfone',# 被操作的应用程序包名appActivity='com.wondershare.drfone.ui.activity.WelcomeActivity',# 启动页面noReset='false',# true--不重置 false--重置app='F:\Pycharm\AppAuto\drfone_v3.2.0.apk', # apk文件所在路径chromedriverExecutable = "F:\Pycharm\AppAuto\chromedriver.exe" # chrome 浏览器驱动所在路径
)
# 发送命令给 appium server
driver = webdriver.Remote('http://127.0.0.1:4723', options=UiAutomator2Options().load_capabilities(desired_caps))# 操作:打开 drfone app->点击继续->点击确定->点击 Backup->点击 Next->进入 Backup 网页页面
continue_loc = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("继续")')
WebDriverWait(driver, 5).until(Ec.presence_of_element_located(continue_loc))
continue_btn = driver.find_element(*continue_loc)
# 点击 继续
continue_btn.click()yes_loc = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("确定")')
WebDriverWait(driver, 5).until(Ec.presence_of_element_located(yes_loc))
yes_btn = driver.find_element(*yes_loc)
# 点击 确定
yes_btn.click()backup_loc = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("Backup")')
WebDriverWait(driver, 5).until(Ec.presence_of_element_located(backup_loc))
backup_btn = driver.find_element(*backup_loc)
# 点击 Backup
backup_btn.click()sleep(5)next_loc = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("Next")')
WebDriverWait(driver, 5).until(Ec.presence_of_element_located(next_loc))
next_btn = driver.find_element(*next_loc)# 点击 Next
next_btn.click()sleep(10)
#原生 app 切换到 webview
print("contexts:",driver.contexts) # contexts: ['NATIVE_APP', 'WEBVIEW_com.wondershare.drfone']# 切换 h5 环境
driver.switch_to.context('WEBVIEW_com.wondershare.drfone')
# 实现元素定位---和 webview 定位一样
email_loc = (AppiumBy.ID, 'email')
WebDriverWait(driver, 5).until(Ec.presence_of_element_located(email_loc))
email_input = driver.find_element(*email_loc)
email_input.send_keys("17710258895@163.com")
time.sleep(2)# webview 切换到原生 app
driver.switch_to.context('NATIVE_APP')