python appium 异常捕获处理以及元素定位xpath参数变量

一、异常捕获处理

1、捕捉异常可以使用try/except语句
try/except语句用来检测try语句块中的错误,从而让except语句捕获异常信息并处理。
如果你不想在异常发生时结束你的程序,只需在try里捕获它。
语法:

try:
    正常的操作
   ......................
except:
    发生异常,执行这块代码
   ......................
else:
    如果没有异常执行这块代码

注意:如代码中需要捕获异常之外执行while循环,那么while语句顶格,try语句空一个tab键
2、捕获万能异常

try....
except Exception:
	...
else

3、捕获异常说明

try:
    xx
except Exception as e:
   print("有错误:", e)
else:
   xx

4、try-finally 语句
try-finally 语句无论是否发生异常都将执行最后的代码。

try:
<语句>
finally:
<语句>    #退出try时总会执行
raise

二、xpath参数变量

xpath的谓语条件(Predicate)
1、所谓"谓语条件",就是对路径表达式的附加条件。
所有的条件,都写在方括号"[]"中,表示对节点进行进一步的筛选。
详细语法查看:https://www.runoob.com/xpath/xpath-syntax.html
2、参数变量
(1)一般情况下,直接用数字指定元素位置

 el3 = driver.find_element_by_xpath(
    "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.GridView/android.widget.FrameLayout[4]/android.widget.ImageView")
    el3.click()

(2)选取属于 xx子元素的最后一个 xx元素

android.widget.FrameLayout[last()]

(3)对于android.widget.FrameLayout[4]中的数字,可以用参数代替,利用format()方法

el3 = driver.find_element_by_xpath(
 "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.GridView/android.widget.FrameLayout[{}]/android.widget.ImageView".format(i))
    el3.click()

三、完整测试脚本

实现app上对用户进行注册,这里着重处理用户上传头像时出现的错误,进行了两次异常捕获,防止上传的图片不符合要求引发程序中断不再执行,同时进行了上传图片不在同一个地方点击,实现了上传不同图片的功能。
在这里插入图片描述

import time
from time import sleep

from appium import webdriver

caps = {}
caps["noReset"] = "true"
caps["fullReset"] = "false"

caps["unicodeKeyboard"] = "true"
caps["resetKeyBoard"] = "true"

caps["platformName"] = "Android"
caps["platformVersion"] = "5.1.1"
caps["deviceName"] = "127.0.0.1:62001"
caps['udid'] = "127.0.0.1:62001"
caps["appPackage"] = "com.xx.xx"
caps["appActivity"] = "com.xx.xx.main.view.MainActivity"

driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)


el1 = driver.find_element_by_xpath(
    "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.View/android.widget.ScrollView/android.widget.RelativeLayout/android.widget.RelativeLayout[2]/android.widget.LinearLayout[3]/android.widget.ImageView")
el1.click()

#进入循环操作
i = int(1)
while i < 5000:
    el1 = driver.find_element_by_id("com.xx.ego:id/tv_picture")
    el1.click()
    el2 = driver.find_element_by_id("com.xx.ego:id/tv_select_album")
    el2.click()
#图片路径参数化
    el3 = driver.find_element_by_xpath(
    "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.GridView/android.widget.FrameLayout[{}]/android.widget.ImageView".format(i))#图片路径参数化
    el3.click()
    el4 = driver.find_element_by_id("com.xx.ego:id/btn_crop_confirm")
    el4.click()
    el5 = driver.find_element_by_id("com.xx.ego:id/tv_name")
    el5.click()
    el6 = driver.find_element_by_id("com.xx.ego:id/et_data")
    el6.click()
    el6.send_keys("A注册%d" % i)
# el6.send_keys("双机测试00000")
    el6.click()
    el7 = driver.find_element_by_id("com.xx.ego:id/btn_right")
    el7.click()
    el8 = driver.find_element_by_id("com.xx.ego:id/tv_valid_period")
    el8.click()
    el9 = driver.find_element_by_xpath(
    "/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.view.View/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.TextView")
    el9.click()

    el1 = driver.find_element_by_id("com.xx.ego:id/tv_group")
    el1.click()
    sleep(3)
    el2 = driver.find_element_by_xpath(
        "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.view.View[2]/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.TextView[1]")
    el2.click()
    el13 = driver.find_element_by_id("com.xx.ego:id/tv_save")
    el13.click()
    sleep(23)
    #捕获异常,
    try:
        el14 = driver.find_element_by_id("com.xx.ego:id/tv_positive")
        el14.click()
    except Exception as e:
        print("有错误:", e)#输出异常,发现图片上传有误,回去重新下标+1,选取新图片上传
        i+=1
        el50 = driver.find_element_by_xpath(
        "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout[1]/android.widget.RelativeLayout")
        el50.click()
        el51 = driver.find_element_by_id("com.xx.ego:id/tv_select_album")
        el51.click()
        el52 = driver.find_element_by_xpath(
        "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.GridView/android.widget.FrameLayout[{}]/android.widget.ImageView".format(i))
        el52.click()
        el53 = driver.find_element_by_id("com.xx.ego:id/btn_crop_confirm")
        el53.click()
        el5 = driver.find_element_by_id("com.xx.ego:id/tv_save")
        el5.click()

        sleep(23)
        #再次捕获异常,
        try:
            el14 = driver.find_element_by_id("com.xx.ego:id/tv_positive") #点击继续注册
            el14.click()
        except:
            print("有错误:", e)#输出异常,发现图片上传有误,回去重新下标+1,第二次选取新图片上传
            i+=1
            el50 = driver.find_element_by_xpath(
                "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout[1]/android.widget.RelativeLayout")
            el50.click()
            el51 = driver.find_element_by_id("com.xx.ego:id/tv_select_album")
            el51.click()
            el52 = driver.find_element_by_xpath(
                "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.GridView/android.widget.FrameLayout[{}]/android.widget.ImageView".format(
                    i))
            el52.click()
            el53 = driver.find_element_by_id("com.xx.ego:id/btn_crop_confirm")
            el53.click()
            el5 = driver.find_element_by_id("com.xx.ego:id/tv_save")
            el5.click()
            sleep(23)
            el14 = driver.find_element_by_id("com.xx.ego:id/tv_positive")  # 点击继续注册
            el14.click()
            print("循环执行%d" % i)
            print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
            #如果程序执行顺利,则能走到这一步,返回while循环执行下一个注册,如果程序执行失败,则在【点击继续注册】这一步骤中中断
            i += 1
        else:#第二次无异常,执行下方程序
            print("循环执行%d" % i)
            print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
            i += 1

    else:#第一次无异常,执行下方程序
        print("循环执行%d" % i)
        print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
        i += 1




  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值