Appium脚本(2):元素检测
场景:
有的按钮在第一次打开时显示,之后就不显示了,如更新提示、特性介绍等,面对这样的场景写了如下脚本,增加脚本的复用性。
no_element_exception_2.py
1 from appium import webdriver 2 from selenium.common.exceptions import NoSuchElementException 3 4 desired_caps = {} 5 desired_caps['platformName'] = 'Android' 6 desired_caps['deviceName'] = '127.0.0.1:62001' 7 desired_caps['platforVersion'] = '5.1.1' 8 9 # 真机配置 10 # desired_caps['deviceName']='MX4' 11 # desired_caps['platforVersion']='5.1' 12 # desired_caps['udid']='750BBKL22GDN' 13 14 # desired_caps['app'] = r'C:\python_dir\apps\kaoyan3.1.0.apk' 15 desired_caps['appPackage'] = 'com.tal.kaoyan' 16 desired_caps['appActivity'] = 'com.tal.kaoyan.ui.activity.SplashActivity' 17 18 # 重置开关,默认false,默认每次如第一次安装好的状态 19 desired_caps['noReset'] = 'True' 20 21 driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) 22 driver.implicitly_wait(5) 23 24 25 def check_cancelBtn(): 26 print("check cancel button") 27 try: 28 cancelBtn = driver.find_element_by_id('android:id/button2') 29 except NoSuchElementException: 30 print("no cancel button") 31 else: 32 cancelBtn.click() 33 driver.implicitly_wait(2) 34 35 36 def check_skipBtn(): 37 print("check skip button") 38 try: 39 skipBtn = driver.find_element_by_id('com.tal.kaoyan:id/tv_skip') 40 except NoSuchElementException: 41 print("no skip button") 42 else: 43 skipBtn.click() 44 driver.implicitly_wait(2) 45 46 47 check_cancelBtn() 48 check_skipBtn() 49 driver.find_element_by_id('com.tal.kaoyan:id/login_register_text').click() 50 driver.implicitly_wait(5)