背景
在做UI自動化時,有播放詳情頁的用例,但是發現視頻在播放的時候無法定位到元素或者很慢,了解到appium在動態的頁面實時獲取布局元素導致定位變慢。所以只能將視頻暫停在操作元素,點擊到暫停按鈕又是個問題,通過adb 點擊坐標的話,不同設備就會成為問題。
解決思路
通過 adb shell uiautomator dump 到xml文件push到電腦端,再進行xml解析,識別到指定的元素,獲取到當前元素坐標,再讓adb進行點擊操作。
import xml.etree.ElementTree as ET
# name是當前操作暫停鍵元素id名稱
def dump_xml_return_tap(name):os.system("adb shell uiautomator dump /sdcard/layout.xml")path = os.path.join(BasePath, "xmls")path_file = os.path.join(path, "{}.xml".format(name))os.system("adb pull /sdcard/layout.xml {}".format(path_file))tree = ET.parse(path_file)root = tree.getroot()for i in root.findall(".//node"):if name in i.get("resource-id"):coord = i.get("bounds")res = coord.strip("[]")res = res.replace("][", ",").split(",")logger.info(res)x = int((int(res[0]) + int(res[2])) / 2)y = int((int(res[1]) + int(res[3])) / 2)logger.info("x:{},y:{}".format(x, y))return {"x": x, "y": y}```