读书郎平板定时截图监控平板屏幕内容
现在学生经常需要平板上网课,读书郎家长端有监控平板平板的功能,但是频繁手动操作,也不方便。就用appium实现定时去抓取一下平板使用情况。经过一天的时间,终于实现稳定、定时抓取读书郎平板屏幕截图。里面有一些很实用的知识点
from appium import webdriver import time from appium.webdriver.common.appiumby import AppiumBy from PIL import Image import os import math from appium.webdriver.common.touch_action import TouchAction # C:\Users\think\Nox_share\ImageShare # adb connect 127.0.0.1:62001 # adb shell date "2021-11-23 10:00:00" def pull_pitcure(pic_name): import os result = os.popen("adb shell ls /sdcard/Pictures/*") res = result.read() cnt = 0 for line in res.splitlines(): cmd = "adb pull "+ line + " " + pic_name if line.split(".")[-1] =="jpg": print(cmd) os.popen(cmd) break def time_now_str(): """ 返回当前时间字符串 :return: """ from datetime import datetime now_time = datetime.now() now_time_str = now_time.strftime('%Y%m%d%H%M%S') return now_time_str def time_now_date_str(): """ 返回当前日期字符串 :return: """ from datetime import datetime now_time = datetime.now() now_time_str = now_time.strftime('%Y%m%d') img_dir=os.path.join("image",now_time_str) if not os.path.exists(img_dir): os.makedirs(img_dir) return img_dir def clean_picture(): """ 清理android 图库中的图片 :return: """ import os result = os.popen("adb shell ls /sdcard/Pictures/*") res = result.read() for line in res.splitlines(): cmd = "adb shell rm -rf "+ line if cmd.split(".")[-1] =="jpg": print(cmd, line) os.popen(cmd) print("图库图片清理完成") def wait_element(driver, AppiumBy, value): ele = None cnt = 0 for i in range(100): try: print("查找元素" +value +"_"+ time_now_str()) ele = driver.find_element(AppiumBy, value) except: pass if ele is not None: break if cnt > 50: break cnt = cnt + 1 time.sleep(1) return ele def wait_three_element(driver, AppiumBy1, value1,AppiumBy2, value2,AppiumBy3, value3): ele_normal = None ele_black_screen= None ele_off_line = None cnt = 0 for i in range(100): # 正常截图 try: print("查找元素" +value1+"_"+ time_now_str() ) ele_normal = driver.find_element(AppiumBy1, value1) except: pass if ele_normal is not None: break # 灭屏 try: print("查找元素" +value2 +"_"+ time_now_str()) ele_black_screen = driver.find_element(AppiumBy2, value2) except: pass if ele_black_screen is not None: break # ele_off_line try: print("查找元素" +value3 +"_"+ time_now_str() ) ele_off_line = driver.find_element(AppiumBy3, value3) except: pass if ele_off_line is not None: break if cnt > 50: break cnt = cnt + 1 time.sleep(1) return ele_normal,ele_black_screen,ele_off_line #os.popen("appium --address 127.0.0.1 --port 4723 --session-override --log-timestamp --local-timezone --no-reset") # appium --address 127.0.0.1 --port 4723 --session-override --log-timestamp --local-timezone --no-reset --command-timeout 6000 def cap(): clean_picture() desired_caps = {'platformName': 'Android', 'deviceName': '127.0.0.1:62001', 'platformVersion': '7.1.2', 'newCommandTimeout':'6000', 'appPackage': 'com.readboy.rbmanager', 'appActivity': 'com.readboy.rbmanager.ui.activity.SplashActivity', 'unicodeKeyboard': True, 'resetKeyboard': True} driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) time.sleep(10) # wait_element(driver,AppiumBy.XPATH,'//*[@text="设备"]').click() # wait_element(driver,AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("设备")').click() wait_element(driver,AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("设备")').click() wait_element(driver,AppiumBy.XPATH,'//*[@text="平板截屏"]').click() #driver.start_activity("com.readboy.rbmanager","com.readboy.rbmanager/.modeule.screenshot.ScreenShotActivity") # driver.start_activity() wait_element(driver,AppiumBy.XPATH,'//*[contains(@text,"截屏查看孩子此刻的学习设备使用情况")]/preceding-sibling::android.widget.RelativeLayout/android.widget.ImageView').click() ele_normal,ele_black_screen,ele_off_line = wait_three_element(driver,AppiumBy.XPATH,'//*[@resource-id="com.readboy.rbmanager:id/app_name"]', AppiumBy.XPATH,'//*[@resource-id="com.readboy.rbmanager:id/app_title"]', AppiumBy.XPATH,'//*[@text="设备离线或网络不稳定,请稍后再试"]') if ele_normal is not None: app_name = ele_normal.text.replace(" ","").replace("/","_") print(app_name) #图片 ele = wait_element(driver, AppiumBy.XPATH, '//*[@resource-id="com.readboy.rbmanager:id/imageview"]') #获取截图坐标 ele = wait_element(driver, AppiumBy.XPATH, '//*[@resource-id="com.readboy.rbmanager:id/imageview"]') min_x = ele.location['x'] + math.floor(ele.size['width'] / 2) min_y = ele.location['y'] + math.floor(ele.size['height'] / 2) #单击截图,显示大图 TouchAction(driver).tap(x=min_x, y=min_y).perform() time.sleep(5) #长按图片,出现保存按钮 TouchAction(driver).long_press(x=min_x, y=min_y, duration=2000).release().perform() #保存图片 wait_element(driver, AppiumBy.XPATH, '//*[@text="保存图片"]').click() pic_name = time_now_date_str()+"\\"+app_name+time_now_str() + ".jpg" time.sleep(2) print(pic_name) pull_pitcure(pic_name) if ele_black_screen is not None: text = ele_black_screen.text print(text) if ele_off_line is not None: text = ele_off_line.text print(text) exit() #截屏正常 OK text = wait_element(driver, AppiumBy.XPATH,'//*[@resource-id="com.readboy.rbmanager:id/app_name"]').text # OK print(text) ele = wait_element(driver, AppiumBy.XPATH,'//*[@resource-id="com.readboy.rbmanager:id/imageview"]') min_x = ele.location['x'] + math.floor(ele.size['width']/2) min_y = ele.location['y'] + math.floor(ele.size['height']/2) print(min_x,min_y) TouchAction(driver).tap(x=min_x,y=min_y).perform() time.sleep(5) TouchAction(driver).long_press(x=min_x,y=min_y,duration=2000).release().perform() wait_element(driver,AppiumBy.XPATH,'//*[@text="保存图片"]').click() #screenshot_by_element(driver,ele,"abc.png") if __name__ == "__main__": cap()
脚本写好后 ,还需要执行一个批处理文件,每隔120秒执行一次脚本
@echo off for /l %%a in (1, 1, 5000) do ( echo %%a D:\work\Stock\project\readboy\venv\Scripts\python.exe D:/work/Stock/project/readboy/readboy_cap.py TIMEOUT /T 120 ) 'pause
在抓取过程中,还看到有破坏分子进入腾讯会议进行破坏,干扰教学
欢迎关注技术微信公众号: