id/name/classname見元素定位如下案例node
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019-06-21 16:31 # @File : kyb_login.py ''' 經過調用capability.py文件登陸考研幫app ''' from find_element.capability import driver,NoSuchElementException def login(): driver.find_element_by_id('com.tal.kaoyan:id/login_email_edittext').clear() # driver.find_element_by_name('請輸入用戶名').clear() # driver.find_element_by_class_name('android.widget.EditText').click() driver.find_element_by_id('com.tal.kaoyan:id/login_email_edittext').send_keys('自學網2018') driver.find_element_by_id('com.tal.kaoyan:id/login_password_edittext').send_keys('zxw2018') driver.find_element_by_id('com.tal.kaoyan:id/login_login_btn').click() try: myself=driver.find_element_by_id('com.tal.kaoyan:id/mainactivity_button_mysefl') except NoSuchElementException: login() else: myself.click() driver.find_element_by_id('com.tal.kaoyan:id/activity_usercenter_username').click() login()
用Appium-desktop查看頁面元素或者用SDK自帶的uiautomatorviewer.bat定位頁面元素python
list定位以下案例android
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019-06-20 11:35 # @Author : zhouyang # @File : kyb_test.py ''' 功能:在模擬器上安裝app而後打開 ''' ''' 複製粘貼當前行Ctrl+D 註釋Ctrl+/ 運行當前腳本Ctrl+Shift+f10 方法定義跳轉Ctrl+B ''' from appium import webdriver from selenium.common.exceptions import NoSuchElementException desired_caps={} desired_caps['platformName']='Android' # desired_caps['platformVersion']='4.4.2' desired_caps['platformVersion']='5.1.1' # desired_caps['deviceName']='127.0.0.1:62001' desired_caps['deviceName']='127.0.0.1:62025' desired_caps['app']=r'D:\360安全瀏覽器下載\apk\kaoyan3.1.0.apk' desired_caps['appPackage']='com.tal.kaoyan' desired_caps['appActivity']='com.tal.kaoyan.ui.activity.SplashActivity' # desired_caps['noReset']='True' desired_caps['noReset']='False'#至關於首次登陸 #帳號涉及中文,因此須要如下設置 desired_caps['unicodeKeyboard']='True' desired_caps['resetKeyboard']='True' desired_caps['automationName']='uiautomator2' driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps) driver.implicitly_wait(3) def check_cancelBtn(): print('start check cancelBtn') try: cancelBtn = driver.find_element_by_id('android:id/button2') except NoSuchElementException: print('no cancelBtn') else: cancelBtn.click() def check_skipBtn(): print('start check skipBtn') try: skipBtn = driver.find_element_by_id('com.tal.kaoyan:id/tv_skip') except NoSuchElementException: print('no skipBtn') else: skipBtn.click() check_cancelBtn() check_skipBtn()
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019-06-24 15:17 # @Author : zhouyang # @File : by_relative.py ''' 相對定位及list定位 注:該案例用於desired_caps['noReset']='False'首次登陸 功能:選擇頭像並註冊用戶 ''' from find_element.capability import driver import random #設置頭像 driver.find_element_by_id('com.tal.kaoyan:id/login_register_text').click()#點擊註冊 root_element=driver.find_element_by_id('com.tal.kaoyan:id/activity_register_parentlayout') root_element.find_element_by_class_name('android.widget.ImageView').click()#點擊頭像按鈕 #獲取全部圖片,選擇第二個 images=driver.find_elements_by_id('com.tal.kaoyan:id/item_image') images[2].click()#索引從0開始 #點擊保存 driver.find_element_by_id('com.tal.kaoyan:id/save').click() #填寫註冊信息 #用戶名 username='zxw2018'+str(random.randint(100,999)) print('uername:%s' %username) driver.find_element_by_id('com.tal.kaoyan:id/activity_register_username_edittext').send_keys(username) #密碼 password='zxw2018'+str(random.randint(100,999)) print('password:%s' %password) driver.find_element_by_id('com.tal.kaoyan:id/activity_register_password_edittext').send_keys(password) #email email='51zxw'+str(random.randint(1000,9999))+'@162.com' print('email:%s' %email) driver.find_element_by_id('com.tal.kaoyan:id/activity_register_email_edittext').send_keys(email) #點擊註冊 driver.find_element_by_id('com.tal.kaoyan:id/activity_register_register_btn').click() #院校選擇 #年份 driver.find_element_by_id('com.tal.kaoyan:id/activity_perfectinfomation_time').click() driver.find_elements_by_xpath('//*[@class="android.widget.TextView"]')[5].click() #院校 driver.find_element_by_id('com.tal.kaoyan:id/perfectinfomation_edit_school_name').click() driver.find_elements_by_id('com.tal.kaoyan:id/more_forum_title')[1].click() driver.find_elements_by_class_name('android.widget.LinearLayout')[1].click() #專業 driver.find_element_by_id('com.tal.kaoyan:id/activity_perfectinfomation_major').click() driver.find_elements_by_id('com.tal.kaoyan:id/major_subject_title')[1].click() driver.find_elements_by_id('com.tal.kaoyan:id/major_group_title')[2].click() driver.find_elements_by_id('com.tal.kaoyan:id/major_search_item_name')[1].click() #進入考研幫 driver.find_element_by_id('com.tal.kaoyan:id/activity_perfectinfomation_goBtn').click()
xpath定位以下案例web
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019-06-24 15:37 # @Author : zhouyang # @File : by_xpath.py ''' 登陸功能 經過xpath定位 1.xpath路徑表達式 / 從根節點選取 //從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置 nodename 選取此節點的全部子節點 . 選取當前節點 .. 選取當前節點的父節點 @ 選取屬性 2.xpath匹配符 * 匹任何元素節點 @* 匹配任何屬性節點 node() 匹配任何類型的節點 ''' from find_element.capability import driver driver.find_element_by_xpath('//android.widget.EditText[@text="請輸入用戶名"]').send_keys('zxw1234') #表示在android.widget.EditText節點下匹配響應的text屬性 driver.find_element_by_xpath('//*[@class="android.widget.EditText" and @index="3"]').send_keys('zxw123456') #*表示在全部節點下匹配class和index屬性 driver.find_element_by_xpath('//android.widget.Button').click() # driver.find_element_by_xpath('//*[@class="android.widget.Button"]').click() #功能相同
UiAutomator元素定位chrome
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019-06-25 16:02 # @Author : zhouyang # @File : by_Uiautomator.py ''' 用Uiautomator定位元素登錄 ''' from find_element.capability import driver # 用id定位 # driver.find_element_by_android_uiautomator\ # ('new UiSelector().resourceId("com.tal.kaoyan:id/login_email_edittext")').send_keys('zwx1234') # 用text定位 # driver.find_element_by_android_uiautomator\ # ('new UiSelector().text("請輸入用戶名")').send_keys('zwx1234') #用classname定位 driver.find_element_by_android_uiautomator\ ('new UiSelector().className("android.widget.EditText")').send_keys('zwx1234') driver.find_element_by_android_uiautomator\ ('new UiSelector().resourceId("com.tal.kaoyan:id/login_password_edittext")').send_keys('zxw123456') driver.find_element_by_android_uiautomator\ ('new UiSelector().resourceId("com.tal.kaoyan:id/login_login_btn")').click()
H5元素定位瀏覽器
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019-06-26 16:24 # @Author : zhouyang # @File : by_H5.py ''' 定位H5頁面上的元素,而且在H5頁面和原生元素見切換 注;須要在app上安裝chrome,定位H5頁面時需用selenium定位方法定位,模擬器的話須要5.0以上,逍遙模擬器 ''' # from appium import webdriver from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait desired_caps={} desired_caps['platformName']='Android' desired_caps['platformVersion']='5.1.1' desired_caps['deviceName']='127.0.0.1:21503' #鏈接逍遙模擬器 desired_caps['app']=r'D:\360安全瀏覽器下載\apk\dr.fone3.2.0.apk' desired_caps['appPackage']='com.wondershare.drfone' desired_caps['appActivity']='com.wondershare.drfone.ui.activity.WelcomeActivity' driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps) driver.implicitly_wait(5) #點擊backup print('click Backup') driver.find_element_by_id('com.wondershare.drfone:id/btnBackup').click() #等待next菜單,點擊 WebDriverWait(driver,10).until(lambda x:x.find_element_by_id('com.wondershare.drfone:id/btnRecoverData')) print('click nextBtn') driver.find_element_by_id('com.wondershare.drfone:id/btnRecoverData').click() #等待H5(webview)頁面 WebDriverWait(driver,8).until(lambda x:x.find_element_by_class_name('android.webkit.WebView')) contexts=driver.contexts print(contexts) #進入H5頁面進行操做 print('switch context') driver.switch_to.context('WEBVIEW_com.wondershare.drfone') print('edit email') driver.find_element_by_id('email').send_keys('2345dgf@163.com') print('click sendBtn') driver.find_element_by_class_name('btn_send').click() #切換webview返回 driver.switch_to.context('NATIVE_APP') driver.find_element_by_class_name('android.widget.ImageButton').click()