一. 定位webview元素
方法一:
1. 获取webview中对应的html页面
谷歌浏览器中输入地址:chrome://inspect(第一次使用要FQ)
前提:手机开启USB调试模式,并且用命令adb devices能够识别设备,app要打开webview页面
2. appium的日志中会显示当前系统的webdriver版本
3. 根据安卓系统的webview版本,去下载对应的chromedriver.exe
chromedriver下载地址:http://npm.taobao.org/mirrors/chromedriver
寻找webview版本39去对应的chromedriver.exe(上述网址中每个版本下都有一个notes.txt,这个文件列出了每个驱动对应的chrome版本),用下载好的chromedriver.exe取替换appium自带的chromedriver,路径是C:\Program Files (x86)\Appium\resources\app\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win下的chromedriver.exe
4. 点击inpect
方法二:获取网页源码:driver.page_source 存成一个html页面再在浏览器里访问定位
方法二使用的前提也是要替换appium中的chromedriver.exe
思路一:保存后利用批处理调用chrome浏览器打开html
在D:\python_workshop\python6_appium\Common下新建一个get_pageSource_1.py
from appium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from appium.webdriver.common.mobileby import MobileBy import time, os #由你主动来告诉appium server,我要操作哪个设备上的哪个app #Desired Capabilities 键值对。键名已经定义好,不能随意更改 #操作对象 的信息准备 desires_caps = {} #操作系统 -目标机 desires_caps["platformName"] = "Android" #系统版本 desires_caps["platformVersion"] = "5.1.1" #设备名字 desires_caps["deviceName"] = "Android Emulator" #app信息 desires_caps["appPackage"] = "com.lemon.lemonban" #首页 desires_caps["appActivity"] = "com.lemon.lemonban.WelcomeActivity" #连接appium server,并告诉其要操作的对象 driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desires_caps) WebDriverWait(driver, 30, 1).until(EC.visibility_of_element_located((MobileBy.ID, "com.lemon.lemonban:id/tv_cancel"))) driver.find_element_by_id("com.lemon.lemonban:id/tv_cancel").click() WebDriverWait(driver, 30, 1).until(EC.visibility_of_element_located((MobileBy.ANDROID_UIAUTOMATOR, "new UiSelector().className(\"android.widget.TextView\").textContains(\"全程班\").resourceId(\"com.lemon.lemonban:id/category_title\")"))) driver.find_element_by_android_uiautomator("new UiSelector().className(\"android.widget.TextView\").textContains(\"全程班\").resourceId(\"com.lemon.lemonban:id/category_title\")").click() WebDriverWait(driver, 60, 0.1).until(EC.visibility_of_element_located((MobileBy.CLASS_NAME, "android.webkit.WebView"))) contexts = driver.contexts driver.switch_to.context(contexts[-1]) html = driver.page_source with open(os.path.split(os.path.abspath(__file__))[0] + "/source.html", "w+", encoding="utf-8") as fs: fs.write(html) time.sleep(5)
在桌面新建一个批处理文件get_pageSource_1.bat,写入如下内容
python D:\python_workshop\python6_appium\Common\get_pageSource_1.py
start chrome.exe D:\python_workshop\python6_appium\Common\source.html
cmd /k
思路二:保存后利用chromedriver驱动浏览器自动打开html
在D:\python_workshop\python6_appium\Common下新建一个get_pageSource_2.py
import appium import selenium from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from appium.webdriver.common.mobileby import MobileBy import time, os #由你主动来告诉appium server,我要操作哪个设备上的哪个app #Desired Capabilities 键值对。键名已经定义好,不能随意更改 #操作对象 的信息准备 desires_caps = {} #操作系统 -目标机 desires_caps["platformName"] = "Android" #系统版本 desires_caps["platformVersion"] = "5.1.1" #设备名字 desires_caps["deviceName"] = "Android Emulator" #app信息 desires_caps["appPackage"] = "com.lemon.lemonban" #首页 desires_caps["appActivity"] = "com.lemon.lemonban.WelcomeActivity" #连接appium server,并告诉其要操作的对象 driver_a = appium.webdriver.Remote('http://127.0.0.1:4723/wd/hub',desires_caps) WebDriverWait(driver_a, 30, 1).until(EC.visibility_of_element_located((MobileBy.ID, "com.lemon.lemonban:id/tv_cancel"))) driver_a.find_element_by_id("com.lemon.lemonban:id/tv_cancel").click() WebDriverWait(driver_a, 30, 1).until(EC.visibility_of_element_located((MobileBy.ANDROID_UIAUTOMATOR, "new UiSelector().className(\"android.widget.TextView\").textContains(\"全程班\").resourceId(\"com.lemon.lemonban:id/category_title\")"))) driver_a.find_element_by_android_uiautomator("new UiSelector().className(\"android.widget.TextView\").textContains(\"全程班\").resourceId(\"com.lemon.lemonban:id/category_title\")").click() WebDriverWait(driver_a, 60, 0.1).until(EC.visibility_of_element_located((MobileBy.CLASS_NAME, "android.webkit.WebView"))) contexts = driver_a.contexts driver_a.switch_to.context(contexts[-1]) html = driver_a.page_source with open(os.path.split(os.path.abspath(__file__))[0] + "/source.html", "w+", encoding="utf-8") as fs: fs.write(html) time.sleep(5) driver_s = selenium.webdriver.Chrome() driver_s.maximize_window() driver_s.get(os.path.split(os.path.abspath(__file__))[0] + "/source.html")
在桌面新建一个批处理文件get_pageSource_2.bat,写入如下内容
python D:\python_workshop\python6_appium\Common\get_pageSource_2.py
cmd /k
个人更倾向于第一种方法,其实两种方法在打开html文件耗时方面差不多
方法三:找开发人员获取网页
二. toast提示信息获取
要获取toast信息要满足以下两个要求:
1. appium版本1.6.3+才支持toast获取
而appium server 1.6.3没有可视化界面,解决方案:下载appium-desktop-Setup-1.4.1-ia32.exe
2. 代码中必须指定automationName为:UIAutomator2
3. UIAutomator2只支持安卓版本5.0+
夜神模拟器默认的安卓版本是4.4.2,可以在夜神多开器中创建并启动一个5.1.1的版本
4. 实例
注意:
1. 实际在运行时,发现判断元素可见的方法不能成功(0/3),而判断元素存在的方法是可以的(3/3)
2. 运行前需要注释掉desired_caps["noRest"] = True,以保证是干净的环境(每一次都是未登录的状态)
from appium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from appium.webdriver.common.mobileby import MobileBy from PageObjects.welcome_page import WelcomePage from PageObjects.index_page import IndexPage from PageObjects.login_page import LoginPage #设备信息 desired_caps = {} desired_caps["automationName"] = "UIAutomator2" desired_caps["deviceName"] = "Android Emulator" desired_caps["platformName"] = "Android" desired_caps["platformVersion"] = "5.1.1" desired_caps["appPackage"] = "com.xxzb.fenwoo" desired_caps["appActivity"] = ".activity.addition.WelcomeActivity" driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) #滑屏 WelcomePage(driver).swipe_page() #点击登录 IndexPage(driver).click_login() #输入手机号 LoginPage(driver).input_phoneNumber("13826741232") part_str = '验证码已经发送到手机' xpath_locator = "//*[contains(@text, \'%s\')]" %part_str try: WebDriverWait(driver, 10, 0.1).until(EC.presence_of_element_located((MobileBy.XPATH, xpath_locator))) except: #如果没找到,则打印 print("toast错过了!") else: text = driver.find_element_by_xpath(xpath_locator).text print(text)
运行结果:
验证码已经发送到手机号为138****1232,注意查收,验证码有效时间为60秒,验证码为7201