AutoPy輕松實現python操作android,實現安卓手機自動化操作!


AutoPy文檔

https://pypi.org/project/AutoPy-Android/

跳轉到AutoPy的官方文檔.

 

 

 

 

0 簡介

AutoPy是為python開發者提供的一個安卓插件,由sunny開始學壞開發維護,主要功能為了實現使用python在安卓端完成一些操作,例如點擊,滑動,返回

1 快速開始

下面演示了AutoPy的一個使用例子,該示例實現了在android上畫圓. 其中 a,b 表示圓心坐標, r 表示半徑. 本示例中 使用了 AutoPy 中的懸浮窗接口方法 AutoPy.floatWindowOpenApi() 和 手勢方法 AutoPy.gesturer(). 這兩個方法將在后續的文檔中介紹.

import AutoPy a=535 b=1696 r=100  li=[]  for x in range(a-r,a+r):  y=int((((r**2)-(x-a)**2)**(1/2))+b)  li.append(x)  li.append(y)   for x in range(a+r,a-r,-1):  y=int(-1*(((r**2)-(x-a)**2)**(1/2))+b)  li.append(x)  li.append(y)  li.append(4000)#執行速度  while True:  if AutoPy.floatWindowOpenApi():  AutoPy.floatWindowClose()  AutoPy.gesturer(li) 

2 准備

安裝AutoPy.apk,點擊安裝模塊,開啟無障礙權限,開啟開發者選項顯示指針位置

2.1 安裝模塊

點擊安裝模塊后會在/sdcard/qpython目錄下寫入 AutoPy.py 文件. 在AutoPy.py 文件中包含AutoPy中的所有方法,用戶可以自行修改該文件,進行自定義操作.

用戶可以將 AutoPy.py 文件移動到 用戶自己的項目中去導入..

在本文的最后會給出AutoPy.py的方法實現代碼.

2.2 開啟無障礙輔助權限

AutoPy依賴android的無障礙輔助權限進行自動化操作.

Android 的輔助模式(Accessibility)功能非常的強大。基本上被獲取到授權之后,可以監聽手機上的任何事件,例如:屏幕點擊、窗口的變化、以及模擬點擊、模擬系統按鍵等等。

無障礙功能實現 點擊 滑動 手勢 等全局操作.

2.3 顯示指針位置
PS:AutoPy不依賴此項功能,這項功能打開才能 "看到" 點擊效果
  1. 打開Android手機“設置”APP,點擊“關於手機”
  2. 版本號上連續點擊5次,打開“開發者選項”。也有些手機在“關於手機”里找不到“版本號”條目,那么也可以點擊其他類似的條目,比如“軟件版本”
  3. 在“設置”APP里找到“開發者選項”,打開它。
  4. 打開“指針位置”,這個時候可以在手機頂部看到以下一行說明:P:0/0 X:0/0 Y:0/0 Xv:0:0 Yv:0:0 Prs:0:0 Size:0:0。其中的X/Y冒號后面的值就是絕對坐標,這行表示坐標原點(0,0),也就是手機左上角。這個時候在屏幕上點擊某個位置,就可以看到該位置的坐標;如果在屏幕上划動,也可以看到划動的軌跡。
  5. 比如用手指按在“某按鈕”上,如下圖所示,界面上顯示一橫一豎兩條線,中間有個交叉的點,手機頂部有一行說明:P:1/1 X:458 Y:647 Xv:0:0 Yv:0:0 Prs:0.25 Size:0.2。這說明這個點的X坐標是458.6,Y坐標是647.7。
  6. 有了坐標值,就可以利用它來做一些簡單的自動化。比如利用命令AutoPy.tap(458,647)點擊坐標(458,647),從而自動打開“某按鈕”。

3 測試

完成准備工作后,點測試按鈕,測試相應功能,若功能正常,請繼續后面操作,若功能無響應,則檢查准備工作

點擊 AutoPy的高級按鈕可以打開AutoPy的測試界面, 如果你確保你已經打開了無障礙,顯示指針位置 等各種權限.那么你點擊測試按鈕時會看到相應的效果. 每一個測試按鈕都有對應的相關的python方法接口,可以使用python 靈活的對方法編程.

導入

完成上述操作后,模塊路徑/sdcard/qpython/AutoPy.py 進入目錄導入模塊

$ cd /sdcard/qpython #進入目錄 $ python #啟動python $
>>> import AutoPy #導入模塊 >>>

使用

AutoPy.tap(X,Y)

模擬點擊指定位置

參數 類型 說明
X int 點擊位置x坐標
Y int 點擊位置y坐標

示例: 某 鐵匠鋪游戲 制作材料等均需要用手進行快速點擊,所謂能者多得.勤勞致富. 例: 刷500鐵 該功能用 AutoPy 編程實現即用:

import AutoPy #導入模塊  number=500 #點擊次數 coordinate={  #按鈕坐標位置  "x":400,  "y":400, } for i in range(number):  AutoPy.tap(coordinate["x"],coordinate["y"])  #點擊(400,400)位置

上述代碼輕松實現刷鐵工作的自動化,並且AutoPy的響應速度十分的快.

AutoPy.swipe(x1,y1,x2,y2,t)

模擬滑動操作

參數 類型 說明
x1 int 起始位置x坐標
y1 int 起始位置y坐標
x2 int 結束位置x坐標
y2 int 結束位置y坐標
t int 執行時間(默認為8000毫秒)

示例: 某 畫板 工具上用手畫直線十分的難畫直,並且長度無法精確控制.

例: 畫邊長為500的正方形 畫一個正方形需要它的邊長定型尺寸以外,我們還需要它的定位尺寸 也就是我們把 邊長500 的正方形放在哪個位置. 以右下角角點定位為例. 因為要畫4條邊的正方形,所有需要調用4次AutoPy.swipe()方法.

該功能用 AutoPy 編程實現即用:

import AutoPy #導入模塊  Corner_point={  #右下角角點坐標,可以方便的修改坐標位置  "x":100,  "y":100, }  lenth=500  AutoPy.swipe(Corner_point["x"],Corner_point["y"],Corner_point["x"]+lenth,Corner_point["y"]) #畫第一條邊 AutoPy.swipe(Corner_point["x"]+lenth,Corner_point["y"],Corner_point["x"]+lenth,Corner_point["y"]+lenth) #畫第二條邊 AutoPy.swipe(Corner_point["x"]+lenth,Corner_point["y"]+lenth,Corner_point["x"],Corner_point["y"]+lenth) #畫第三條邊 AutoPy.swipe(Corner_point["x"],Corner_point["y"]+lenth,Corner_point["x"]+lenth,Corner_point["y"]) #畫第四條邊 

AutoPy.gesturer([x1,y1,x2,y2,...,t])

連續滑動操作

注意:AutoPy.gesturer方法只有一個list(列表)參數

AutoPy.gesturer只能傳入一個列表

其中list列表數據格式如下

參數 類型 說明
x1 int 起始位置x坐標
y1 int 起始位置y坐標
x2 int 結束位置x坐標
y2 int 結束位置y坐標
... int ...
t [必須] int 起始位置到結束位置持續時間

示例: 上一個方法介紹了怎樣畫一個正方形,有些小伙伴就要問了,那怎么樣畫一個曲線呢,連續光滑的曲線.於是便有了AutoPy.gesturer()方法,可以傳入多個坐標,實現畫出連續光滑的曲線.可以制作迷宮,連連看,等游戲的自動化解題. 例: 畫r100圓 上面說到r100是圓的定型尺寸,所以還需要圓的定位尺寸,即圓放在哪個位置.

該功能用 AutoPy 編程實現即用:

import AutoPy a=535 b=1696 r=100  li=[]  for x in range(a-r,a+r):  y=int((((r**2)-(x-a)**2)**(1/2))+b)  li.append(x)  li.append(y)   for x in range(a+r,a-r,-1):  y=int(-1*(((r**2)-(x-a)**2)**(1/2))+b)  li.append(x)  li.append(y)  li.append(4000)#執行速度  AutoPy.gesturer(li) 

其中a,b是圓心坐標,r是半徑.

AutoPy.capturer()

三指下滑截圖,沒有三指截圖功能機型無效

三指截圖功能是3個異步的手勢同時滑動模擬出了3條路徑 截圖后的文件會儲存到手機自帶的截圖文件夾里面,例/sdcard/截圖文件夾/.需要自行獲取路徑+文件名來配合使用.

示例: 如我們需要自動錄制3幀的視頻,則需要調用3次截圖功能,最后用圖片來合成視頻. 例: 每10秒 截1幀

該功能用 AutoPy 編程實現即用:


import AutoPy #導入模塊 import time  #**例: 每10秒 截`1幀`圖** num=3 for i in range(num):  AutoPy.capturer()#三指下滑實現截圖  time.sleep(10) 

AutoPy.StartServer()

截圖服務

在上面我們介紹到截圖的圖片會放到手機自帶的文件夾里面去,對於我們去取這個圖片就會造成極大的不便. 本方法實現的主要功能是監控手機的截圖文件夾,如有新截圖會自動返回並且將新圖片放到用戶自定義的位置去,方便用戶使用截圖進行分析.

示例: 以小米手機為例,將監控的截圖文件夾的新文件,自動監控並自動轉移至我們工程目錄下. 小米手機的截圖目錄:/sdcard/DCIM/Screenshots/ 工程目錄:/sdcard/qpython/

例: 監控截圖到工程目錄下

該功能需要改寫 AutoPy.py的AutoPy.StartServer()方法 編程實現即用:


import AutoPy #導入模塊  def StartServer(_Screenshots_='/sdcard/DCIM/Screenshots/',_newdir_='/sdcard/qpython/'):  _thread.start_new_thread(ListenServer, (_Screenshots_,_newdir_) )  StartServer() 

如上便能自動監控截圖並且自動返回到項目目錄中.

AutoPy.HOME()

模擬主頁鍵

主頁鍵就是返回桌面.

import AutoPy #導入模塊 AutoPy.HOME()#模擬主頁鍵

AutoPy.RECENTS()

模擬多任務鍵

import AutoPy #導入模塊 AutoPy.RECENTS()#模擬多任務鍵

AutoPy.BACK()

模擬返回鍵

import AutoPy #導入模塊 AutoPy.BACK()#模擬返回鍵

AutoPy.Locker()

鎖屏

例: 完成工作后自動鎖屏

import AutoPy #導入模塊 def job():  print("這是一個需要執行5個小時的工作....")  job() print("工作結束.") AutoPy.Locker()#鎖屏 

AutoPy.floatWindowOpenApi()

AutoPy懸浮窗接口

該功能是AutoPy的高級用法,一般用於阻塞當前程序,等待按鈕按下后再運行. 一般用作按鈕的按鈕的點擊事件觸發. 如上面的程序都是執行后馬上運行,不會等待用戶到達指定場景后再運行,該功能就可以解決上面的問題,將等帶用戶點擊開啟懸浮窗后才會執行,即可達到交互的控制.

例: 用戶點擊后進行畫圓操作 使用AutoPy.floatWindowOpenApi()AutoPy.floatWindowClose()方法進行控制. AutoPy目前提供了兩個按鈕的接口服務.可供處理兩個按鈕的點擊事件. 如下用了一個開啟按鈕事件:

import AutoPy a=535 b=1696 r=100  li=[]  for x in range(a-r,a+r):  y=int((((r**2)-(x-a)**2)**(1/2))+b)  li.append(x)  li.append(y)   for x in range(a+r,a-r,-1):  y=int(-1*(((r**2)-(x-a)**2)**(1/2))+b)  li.append(x)  li.append(y)  li.append(4000)#執行速度  while True:  if AutoPy.floatWindowOpenApi():  AutoPy.floatWindowClose()  AutoPy.gesturer(li)  

關閉按鈕寫法與開啟按鈕類似 如下:

import AutoPy a=535 b=1696 r=100  li=[]  for x in range(a-r,a+r):  y=int((((r**2)-(x-a)**2)**(1/2))+b)  li.append(x)  li.append(y)   for x in range(a+r,a-r,-1):  y=int(-1*(((r**2)-(x-a)**2)**(1/2))+b)  li.append(x)  li.append(y)  li.append(4000)#執行速度  while True:  if AutoPy.floatWindowCloseApi():  AutoPy.floatWindowClose()  AutoPy.gesturer(li)  

錄制教學功能

點擊錄制教學,按住文字可以拖動,當拖動到適當位置的時候,選擇執行點擊或拖動功能,AutoPy會自動記錄下當前的坐標,並且會自動點擊當前選定的位置,會生成一個懸浮窗標記當前位置,可以很方便的實現坐標記錄和python代碼生成.

AutoPy可以自動生成python代碼. 生成一段代碼如下: 示例:

import AutoPy,time  dy=80 #y坐標偏移調整 dx=0 #x坐標偏移調整 t=1000 #毫秒數轉秒數  """ 此腳本由 AutoPy自動生成, 位置坐標和時間延遲可能不准確, 請手動微調! """  AutoPy.tap(324+dx,1692+dy) time.sleep(1) AutoPy.tap(559+dx,1680+dy) time.sleep(750/t) AutoPy.tap(343+dx,1600+dy) time.sleep(1363/t) AutoPy.tap(600+dx,1731+dy) time.sleep(708/t) AutoPy.tap(696+dx,1669+dy) time.sleep(1100/t) AutoPy.tap(241+dx,1835+dy) time.sleep(959/t) AutoPy.tap(557+dx,1845+dy) time.sleep(904/t) AutoPy.tap(685+dx,1804+dy) time.sleep(821/t) AutoPy.tap(781+dx,1676+dy) time.sleep(779/t) AutoPy.tap(855+dx,1555+dy) time.sleep(930/t) AutoPy.tap(455+dx,1949+dy) time.sleep(2147/t) AutoPy.swipe(681+dx,1844+dy,396+dx,1784+dy,2683) time.sleep(396/t) AutoPy.tap(396+dx,1784+dy) time.sleep(2216/t) AutoPy.tap(640+dx,1796+dy) time.sleep(2672/t) AutoPy.swipe(281+dx,1853+dy,571+dx,1864+dy,1326) time.sleep(571/t) AutoPy.swipe(145+dx,1692+dy,145+dx,1692+dy,754) time.sleep(145/t) AutoPy.tap(120+dx,1941+dy) time.sleep(8120/t) AutoPy.tap(105+dx,1770+dy) time.sleep(1463/t) AutoPy.tap(699+dx,1871+dy) time.sleep(3436/t)

以上代碼由AutoPy自動生成.

最后所有方法實現代碼

# -*- coding: utf-8 -*-  import sys import time import os,_thread  if sys.version[0] == 2:  from urllib2 import urlopen else:  from urllib.request import urlopen  server_url = 'http://127.0.0.1:33445?code='  def dian(x,y):  f=urlopen(server_url+'0,'+str(x)+','+str(y))  f.close()  print(time.ctime()[11:19]+' [AutoPy]點擊執行成功')  time.sleep(0.1)  def tuo(x1,y1,x2,y2,t=1000):  f=urlopen(server_url+'1,'+str(x1)+','+str(y1)+','+str(x2)+','+str(y2)+','+str(t))  f.close()  print(time.ctime()[11:19]+' [AutoPy]滑動執行成功')  time.sleep(0.0023*t)  def swipe(x1,y1,x2,y2,t=1000):  f=urlopen(server_url+'1,'+str(x1)+','+str(y1)+','+str(x2)+','+str(y2)+','+str(t))  f.close()  print(time.ctime()[11:19]+' [AutoPy]滑動執行成功')  time.sleep(0.0023*t)  def tap(x,y):  f=urlopen(server_url+'0,'+str(x)+','+str(y))  f.close()  print(time.ctime()[11:19]+' [AutoPy]點擊執行成功')  time.sleep(0.1)  def click(x,y):  f=urlopen(server_url+'0,'+str(x)+','+str(y))  f.close()  print(time.ctime()[11:19]+' [AutoPy]點擊執行成功')  time.sleep(0.1)  def capturer():  f=urlopen(server_url+'2,')  f.close()  print(time.ctime()[11:19]+' [AutoPy]截屏執行成功')  def ListenServer(_Screenshots_='/sdcard/DCIM/Screenshots/',_newdir_='/sdcard/qpython/'):#暫時無用  _Screenshots_=_Screenshots_  _newdir_=_newdir_  t=os.listdir(_Screenshots_)  while (True):  nt=os.listdir(_Screenshots_)  li=list(set(nt)-set(t))  if(len(li)!=0):  t=os.listdir(_Screenshots_)  print('監控到截圖',li)#監控到截圖  os.system('mv '+_Screenshots_+li[0]+' '+_newdir_+'AutoPy_Screenshots.jpg')    def StartServer(_Screenshots_='/sdcard/DCIM/Screenshots/',_newdir_='/sdcard/qpython/'):  _thread.start_new_thread(ListenServer, (_Screenshots_,_newdir_) )   def gesturer(li):  li=li  f=urlopen(server_url+'3,'+','.join(map(str,li)))  f.close()  print(time.ctime()[11:19]+' [AutoPy]手勢執行成功')  def HOME():  f=urlopen(server_url+'HOME,')  f.close()  print(time.ctime()[11:19]+' [AutoPy]主頁鍵執行成功')  def BACK():  f=urlopen(server_url+'BACK,')  f.close()  print(time.ctime()[11:19]+' [AutoPy]返回鍵執行成功')  def RECENTS():  f=urlopen(server_url+'RECENTS,')  f.close()  print(time.ctime()[11:19]+' [AutoPy]菜單鍵執行成功')    def Locker():  f=urlopen(server_url+'Lock,')  f.close()  print(time.ctime()[11:19]+' [AutoPy]鎖屏執行成功')  def floatWindowOpenApi():  f=open('/sdcard/AutoFloatWindow.info','r+').read()  if f=='1':  return True  #o=open('/sdcard/AutoFloatWindow.info',1w+').close()   def floatWindowCloseApi():  f=open('/sdcard/AutoFloatWindow.info','r+').read()  if f=='0':  return True  def floatWindowClose():  o=open('/sdcard/AutoFloatWindow.info','w+').close() def systemCapturer():  f=urlopen(server_url+'capturer,')  f.close()  print(time.ctime()[11:19]+' [AutoPy]返回鍵執行成功')   #作者:sunny開始學壞 
更多功能持續開發中......
QQ群:540717901

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM