如果是加载界面的过程中报错

这种,如果代码没有问题,就是页面没有加载出来执行代码错误,需要设置延迟时间

pycharm on ssh 为什么每次启动项目都要 update python interpreter_测试工具

import time
time.sleep(10)

pycharm on ssh 为什么每次启动项目都要 update python interpreter_python_02

执行当前文件

如果遇到执行py文件,执行文件错误的情况下,可能执行的文件不是当前文件

快捷键Ctrl+Shift+F10

pycharm on ssh 为什么每次启动项目都要 update python interpreter_包名_03

怎么在代码内启动其他应用并且输入包名和界面名称

driver.start_activity("包名","界面名")

time.sleep(3)#等待3秒
driver.start_activity("com.baidu.searchbox","com.baidu.searchbox.MainActivity")#启动百度应用
time.sleep(2)
print(driver.current_package)#获取包名
print(driver.current_activity)#获取启动页面名

通过代码关闭应用和驱动对象

#关闭app和驱动对象(应用场景)有时候我们需要关闭某个应用程序后,在打开新的应用,相当于上滑退出应用,启动新应用)
#方法:
driver.close_app()#关闭当前操作的app,不会关闭驱动对象(可以获取包名)
driver.quit()#关闭驱动对象,同时关闭所有关联的app(不能在获取包名,因为所有程序都被杀掉了)

安装应用/卸载应用(及验证是否安装应用)

#场景:有一些应用市场软件可能会有一个按钮(安装按钮,卸载按钮,更新按钮),如果某个程序已经安装就卸载,繁殖就安装
if driver.is_app_installed("com.baidu.searchbox"):#判断是否安装了百度()里面传的是包名
    driver.remove_app("com.baidu.searchbox")#安装了,执行卸载命令
else:#反之
    deriver.install_app("/users/ceshi/baidu.apk")#执行安装命令()里面填写的是安装地址

应用进入后台

有些应用会设置进入后台多少秒钟,热启动就需要重新验证的情况,所以模拟该场景就需要当前代码

#场景:模拟app-HOME键,将app放到后台,并热启动(PS:如果应用没有被杀掉就是热启动,如果已经杀掉就是冷启动)
print("----------准备进入后台-------------")#这个是为了方便查看进入后台代码执行情况
driver.background_app(5)#进入后台5秒,并自动回到前台
print("-----------已经回到前台-------------")

启动本地程序

启动报错(代码也是正确的,但是就是报错,后来,我重新启动了appium发现可以了)

pycharm on ssh 为什么每次启动项目都要 update python interpreter_android_04

元素定位

单个元素定位

pycharm on ssh 为什么每次启动项目都要 update python interpreter_包名_05

pycharm on ssh 为什么每次启动项目都要 update python interpreter_python_06

这个element代表定位单个元素

#定位一个元素(重要)
#应用场景:想要对某个按钮进入点击操作,想要对输入框进行输入,想要获取文本框内容,定位元素是自动化操作必须要使用的方法
        #只有获取元素之后,才能对元素进行定位操作(有三个方法)
 #第一个:通过id定位元素(id_value:对应的id是resource-id的选择器)
driver.find_element(by=AppiumBy.ID, value="com.gaomon.smart:id/btn_login").click()#通过id定位login按钮,定进行点击操作
driver.find_element(by=AppiumBy.CLASS_NAME,value="android.widget.TextView").click()#通过CLASS_NAME定位login按钮,定进行点击操作
driver.find_element(by=AppiumBy.XPATH,value="//*[@text='Cao Right]").click()#通过XPATH定位login按钮,定进行点击操作
driver.find_element(by=AppiumBy.ACCESSIBILITY_ID,value="toolbar_navigation").click()#通过ACCESSIBILITY_ID定位content-desc属性

一组元素定位

如果遇到元素属性一样的情况,就需要用到一组元素定位的方法去定位元素的位置

pycharm on ssh 为什么每次启动项目都要 update python interpreter_python_07

pycharm on ssh 为什么每次启动项目都要 update python interpreter_测试工具_08

pycharm on ssh 为什么每次启动项目都要 update python interpreter_元素定位_09

elements代表定位一组元素

[2]代表我定位的是列表中第二个元素

#如果元素属性值相同,点击自己想要的元素,需要先获取元素的位置(需要定位一组元素,在一组元素中定位启动的某个元素的属性)
driver.find_elements(by=AppiumBy.CLASS_NAME,value="android.widget.RelativeLayout")[2].click()
print("------进入控制面板-------------")

还有另外一种情况,就是你不确定这组元素中哪个是自己需要的哪个,那就需要把元素打印出来,就可以直观的看到自己想要的元素的位置

titles=driver.find_elements(by=AppiumBy.CLASS_NAME,value="android.widget.RelativeLayout")
for title in titles:#打印元素列表
    print(title.text)

pycharm on ssh 为什么每次启动项目都要 update python interpreter_android_10

pycharm on ssh 为什么每次启动项目都要 update python interpreter_android_11

如果需要通过ID,只需要把方法名和里面的字符串就可以了

titles=driver.find_elements(by=AppiumBy.ID,value="com.gaomon.smart:id/tv_choose_home_name")
for title in titles:#打印元素列表
    print(title.text)

如果通过XPATH

shebeis=driver.find_elements(by=AppiumBy.XPATH,value="//*[@text='Test Home']")
for shebei in shebeis:
    print(shebei.text)

只有一个text,没有相同值的时候

pycharm on ssh 为什么每次启动项目都要 update python interpreter_python_12

多个元素都有相同的text的时候

shebeis=driver.find_elements(by=AppiumBy.XPATH,value="//*[contains(@text,'Home')]")
for shebei in shebeis:
    print(shebei.text)

pycharm on ssh 为什么每次启动项目都要 update python interpreter_python_13

python注释代码的几种方法

多行注释

pycharm on ssh 为什么每次启动项目都要 update python interpreter_android_14

pycharm on ssh 为什么每次启动项目都要 update python interpreter_python_15

单行注释

pycharm on ssh 为什么每次启动项目都要 update python interpreter_包名_16

 python常见的几种报错提示,快速的找到原因

提示给定的参数无法定位的(这两个提示都是因为参数有误无法定位或者没有参数导致的)

提示:exceptions.NOSuchElement Exception:Message:An element could not be located on the page using the given search......

pycharm on ssh 为什么每次启动项目都要 update python interpreter_python_17

pycharm on ssh 为什么每次启动项目都要 update python interpreter_包名_18

滑动操作

# driver.swipe(33,1470,482,498)#没加滑动时间(快滑)
driver.swipe(33,1470,482,498,5000)#加了5000毫秒的滑动时间(慢滑)

 调用的方法

pycharm on ssh 为什么每次启动项目都要 update python interpreter_元素定位_19

元素等待

有时候元素因各种问题导致没有加载出来,无法定位,就需要设置等待时间

        一般是固定时间等待,或者隐式等待

driver.implicitly_wait(30)#隐式等待

import time
time.sleep(5)#固定时间等待
还有一种显式等待(可以百度搜索)

输入中文限制

有时候我们的代码是不可以输入中文的,不会报错,但也不会执行(最近发现可以执行)

但如果觉得有必要可以把这两行代码带上

caps["appium:unicodeKeyboard"] = True
caps["resetKeyboard"]  = True

pycharm on ssh 为什么每次启动项目都要 update python interpreter_包名_20

python中,可执行的操作

.click()#点击
.send_keys("")#对输入框进行输入
.clear()#清空输入框

输出:
print(----------------)

print(对象)#输出元素

print(len(对象))#获取元素总数

for title in 对象
    print(title.text)#对象文本(这个是列表)
print(i.text)#这个是打印单个对象文本