python+uiautomator2+weditor获取页面的所有文本数据

一、导入库

#coding=utf8
import uiautomator2 as u2
import time
import logging
import re
import json
from selenium import webdriver
d = u2.connect('MQS7N19423008360')#
d.click_post_delay = 1.5#

logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(funcName)s - %(message)s')
logger = logging.getLogger(__name__)
logger.info(d)

二、打开程序

def run_app():
    d.press("home")
    d.app_start("com.xunmeng.pinduoduo")#打开程序
    if d.session("com.xunmeng.pinduoduo", attach=True):#判断程序是否打开
        logger.info("已打开程序")
        if d(text="热门").exists(timeout=5):#exists:等待元素出现
            logger.info("当前是打开状态״̬")
            food()
        else:
            d.xpath('//*[@resource-id="com.xunmeng.pinduoduo:id/bqe"]/android.widget.RelativeLayout[1]/android.widget.ImageView[1]').click()
            food()
            
    else:
        print('app打开错误')

三、定位到需要获取的页面然后,获取所有页面元素

file = open('pdd_2.txt', 'a', encoding='utf-8')
for i in range(10):
        d.xpath('//*[@resource-id="com.xunmeng.pinduoduo:id/cak"]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]').click()
        for elem in d.xpath('//android.widget.TextView').all():
            print("Text:",elem.text)#获取当前视图所有文本视图文本
            file.write(str(elem.text) + ' ')#写入txt文件

四、结果


版权声明:本文为weixin_44867493原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。