1. 程式人生 > 實用技巧 >appium自動化之元素屬性是否存在的判斷

appium自動化之元素屬性是否存在的判斷

#判斷元素是否存在
def is_element_exist(element,timeout=1):
    count = 0
    while count < timeout:
        souce = driver.page_source
        if element in souce:
            return  True
        else:
            count += 1
            time.sleep(1)
    return False

實戰例子:

import time
from  appium import webdriver
from selenium.common.exceptions import NoSuchElementException ''' mumu模擬器:抖音綜合操作 ''' # 裝置資訊 desired_caps = { "platformName": "Android", "platformVersion": "6.0.1", "deviceName": "127.0.0.1:7555", "appPackage": "com.ss.android.ugc.aweme", "appActivity": "com.ss.android.ugc.aweme.splash.SplashActivity
", "noReset": True, "unicodeKeyboard": True, "resetKeyboard": True } driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps) # driver.implicitly_wait(10) time.sleep(6) #判斷元素是否存在 def is_element_exist(element,timeout=1): count = 0 while count < timeout: souce = driver.page_source
if element in souce: return True else: count += 1 time.sleep(1) return False # 搜尋操作 def douyin_search(): driver.tap([(752, 68)], 500) # 點選搜尋按鈕 time.sleep(1) driver.find_element_by_id('com.ss.android.ugc.aweme:id/ai4').send_keys('東山桃花山農產品') time.sleep(4) driver.tap([(100, 162), (736, 198)], 500) # 點選第一個搜尋結果 time.sleep(1) driver.swipe(100, 266, 100, 198, 1000) # 去除綜合排序干擾 time.sleep(1) element = driver.find_elements_by_id('com.ss.android.ugc.aweme:id/efw')[0] user_name = element.__getattribute__("text") # 使用者名稱 print(user_name) if user_name == '邳州亞森苗木': # 如果使用者與搜尋結果匹配 driver.find_element_by_id('com.ss.android.ugc.aweme:id/h_y').click() # 取消搜尋內容 time.sleep(1) print("111111") else: # 使用者與搜尋不匹配的話 driver.swipe(100,300,100,200,1000) time.sleep(1) print("8888") elements = is_element_exist('com.ss.android.ugc.aweme:id/hkt') print(elements) if elements == True: #西瓜視訊標籤元素存在 xigua_horizontal_screen_swipe() print("22222222") is_user_element_exist() else:#西瓜標籤不存在 is_user_element_exist() #判斷使用者元素是否存在 def is_user_element_exist(): user_status = is_element_exist('com.ss.android.ugc.aweme:id/efw') print(user_status) while True: elements = is_element_exist('com.ss.android.ugc.aweme:id/hkt') print(elements) if elements == True: # 西瓜視訊標籤元素存在 xigua_horizontal_screen_swipe() else: if user_status == True: # 判斷使用者id元素是否存在 enter_user_homepage() break else: driver.swipe(100, 500, 100, 400, 1000) enter_user_homepage() break #判斷標籤元素是否存在 def is_tag_element_exist(): pass #進入使用者主頁操作 def enter_user_homepage(): for i in range(0,20): user_status = is_element_exist('com.ss.android.ugc.aweme:id/efw') if user_status == True: element = driver.find_elements_by_id('com.ss.android.ugc.aweme:id/efw')[0] user_name = element.__getattribute__("text") print(user_name) if user_name: # 如果有使用者 element.click() # 進入主頁 time.sleep(1) driver.find_element_by_id('com.ss.android.ugc.aweme:id/btt').click() # 進入頭像頁 time.sleep(1) print("33333") driver.tap([(100, 100)], 500) # 退出頭像頁 time.sleep(1) print("44444") driver.tap([(50, 82)], 500) # 退出使用者主頁 time.sleep(3) print("55555") driver.swipe(100, 1198, 100, 198, 1000) # 滑動下一個使用者 time.sleep(1) else: driver.swipe(100,500,100,300,1000) time.sleep(1) #去除與搜尋結果相同的使用者 def user_repeat(): driver.find_element_by_id('com.ss.android.ugc.aweme:id/qi').click() # 取消搜尋內容 time.sleep(1) douyin_search() #西瓜視訊滑動操作 def xigua_horizontal_screen_swipe(): driver.swipe(100,700,100,198,1000) #滑動跳過橫屏西瓜視訊 time.sleep(2) print("66666") if __name__ == '__main__': douyin_search()