对hermit 的API进行简单的封装,做成了这个python moudle

Overview

hermit-py

对hermit 的API进行简单的封装,做成了这个Python Moudle,推荐通过wheel的方式安装。
目前对点击、滑动、模拟输入、找组件、等支持较好,支持查看页面的实时布局信息,再通过布局信息进行点击滑动等操作。 支持剪贴板相关的操作,支持设置剪贴的任意语言内容。解决了adb不能设置为中文的痛点。

未来

如果反响尚可,会持续优化,注入更多新的功能,比如可视化查看组件信息等。

请先看三个演示,再阅读下方的详细说明

演示1:群聊抢红包

import time
from pyhermit import Hermit

hm = Hermit('127.0.0.1:9999')

# 每隔0.1秒监视一次
while not hm._is_clickable('text', '微信红包'):
    time.sleep(0.1)

hm.click_text('微信红包')
# 模拟器配置低,打开红包过程略慢,需要等待1秒钟,才能再开。
hm.click_desc('开', 1)

演示2:打开酷安并快速找到并进入iPhone SE区

import time
from pyhermit import Hermit
# 实例化
hm = Hermit('127.0.0.1:9999')
# 按下home键
hm.action_home()
hm.click_channel([
    {'text': '酷安'}, {'text': '闲聊'},
    {'text': '数码'}, {'text': '平板'},
    {'text': '手机'}, {'text': '苹果'}
])
# 如果没找到,就一直下滑,直到找到为止。
while not hm._is_clickable('text', 'iPhone SE'):
    hm.swipe_up(4)
    time.sleep(0.2)
hm.click_text('iPhone SE')

###演示3: 打开微信,并搜索v2ex

import time
from pyhermit import Hermit

hm = Hermit('127.0.0.1:9999')

hm.click_text('微信', 1)
hm.shell_tap(670, 86)
time.sleep(1)
hm.input('id', 'com.tencent.mm:id/bhn', 'v2ex')
hm.click_id('com.tencent.mm:id/b3b', 1)
time.sleep(5) # 等待搜索结果
hm.swipe_up(5)

实例化

在此之前,请确保要操作设备已经安装了hermit APP。

from pyhermit import Hermit
hm = Hermit('127.0.0.1:9999')
"""
127.0.0.1:9999 是hermit APP 运行的IP地址加端口。
如果是模拟器,需要先连接 adb,再设置端口转发即可,以mumu为例,如下:
"""
adb connect 127.0.0.1:7555
adb forward tcp:9999 tcp:9999
# 看到返回 9999, 就是成功了。

点击与滑动

点击与滑动是最基本的操作了,支持无障碍和root两种方式。
无障碍能够根据页面中 textresource-idcontent-desc进行查找组件并点击。
root方式则可以点击屏幕中任意一个坐标位置,和无障碍能够较好的相互互补。

  • 无障碍的方式点击
# 下面分别演示,通过text、resource-id、和content-desc点击
hm.click_text('酷安', 1)
hm.click_id('com.coolapk.market:id/title_view')
hm.click_desc('搜索')
"""
这三个函数,都有一个缺省参数,sleep,单位为秒,可以等待上一个操作执行。
例如,当你打开一个APP,他有首屏广告时,我们需要等待之后再执行点击。

一个一个click似乎不太过瘾,那么可以尝试用`channel`打一套组合拳。
"""
hm.click_channel([
    {'text': '酷安'}, {'id': 'com.coolapk.market:id/title_view'},
    {'desc': '搜索'},
])
# 它会遍历并执行数组中的操作,自带判断功能,默认5s等待时间(每秒判断一次是否可点击),超时自动下一个。
  • root的点击与滑动
# 申请root权限  
hm.is_root()
# 点击坐标(100, 100)(2,同为缺省参数,等待上一个操作的时间)
hm.shell_tap(100, 100, 2)

# 从(x1, y1)滑动到(x2, y2)
hm.shell_swipe(100, 100, 100, 500)

# 衍生的上下左右滑动(加入了随机数值,防止被检测)  
hm.swipe_up()
hm.swipe_down()
hm.swipe_left()
hm.swipe_right()
# 他们都有一个缺省参数scope,默认为2,表示幅度,范围[1, 10]。 数值越大,范围越大
hm.swipe_up(5)  # 大约滑动半屏距离
hm.swipe_up(10) # 大约滑动满屏距离

全局操作

hm.action_back() # 返回  
hm.action_home() # 按home键 
hm.action_power() # 长按电源键  
hm.action_recents() # 显示最近任务  
hm.action_noticefications() # 拉下通知栏  
hm.action_lock_screen() # 锁屏  
hm.action_quick_settings() # 下拉打开快速设置  
hm.action_split_screen() # 分屏 限制 Android 9.0  
hm.action_screen_shot()  # 截屏 限制 Android 9.0  

剪贴板操作

hm.cliboard_push('法外狂徒')  # 设置剪贴板内容  
hm.cliboard_pull() # 读取剪切板内容

有用的辅助操作

from pyhermit import Hermit
hm = Hermit('127.0.0.1:9999')

# 打印当前界面的布局信息, 可以用来找resource-id、text等,进而进行点击与模拟输入  
print(hm.get_nodes())

# 以下方法均返回 布尔类型
hm.is_root() # 申请并判断是否有root权限  
hm.in_page('加载完毕')  # 判断`加载完毕` 是否再当前页面中 

# 判断当前视图中,是否有text=微信的可点击组件
hm._is_clickable('text', '微信')  
You might also like...
Comments
  • add start_app() to start app by packagename.

    add start_app() to start app by packagename.

    great job, hermit-py with hermit made android automation much easyer.
    thank you soooooo much.
    I'm afraid you have forgot the 'start_app' func which start app through packageName, because in the api document i have found the api '/shell/start'. so i'm trying to fix it, add the func to start app.
    wish you merge it or add the func in your own way.
    Thanks again!

    opened by ScriptBoy2077 1
Releases(2.20)
Owner
LookCos
Go big or go home.
LookCos
Best Buy purchase bot

B3 Best-Buy-Bot. Written in Python NOTICE: Don't be a disgrace to society. Don't use this for any mass buying/reselling purposes. About B3 is a bot th

Dogey11 8 Aug 15, 2022
Nft-maker - Create your own NFT!

nft-maker How to If you're going to use this program, change the pictures in the "images" folder. All images must be of the same resolution and size.

Georgii Arakelian 4 Mar 13, 2022
Python3 script to dump employee information from XING API

XingDumper Python 3 script to dump company employees from XING API. Perfect OSINT tool ;-) The results contain firstname, lastname, position, gender,

LRVT 11 Dec 26, 2022
A Python wrapper around the Soundcloud API

soundcloud-python A friendly wrapper around the Soundcloud API. Installation To install soundcloud-python, simply: pip install soundcloud Or if you'r

SoundCloud 83 Dec 12, 2022
Unofficial instagram API, give you access to ALL instagram features (like, follow, upload photo and video and etc)! Write on python.

Instagram-API-python Unofficial Instagram API to give you access to ALL Instagram features (like, follow, upload photo and video, etc)! Written in Pyt

Vladimir Bezrukov 1 Nov 19, 2021
this is a telegram bot repository, that can stream video on telegram group video chat.

VIDEO STREAM BOT telegram bot project for streaming video on telegram video chat, powered by tgcalls and pyrogram 🛠 Commands: /vstream (reply to vide

levina 319 Aug 15, 2022
A frame to create discord bots (for myself) that uses cogs, JSON, activities, and more.

dpy-frame A frame to create discord bots (for myself) that uses cogs, JSON, activities, and more. NOTE: Documentation is incomplete, so please wait un

Apple Discord 1 Nov 06, 2021
Some python code to make twitter bots ;)

How to set up a twitter bot using python's tweepy library Create a twitter developer account and project Make sure you are logged into your twitter ac

Wael 2 Jan 10, 2022
Shuffle and add items from jellyfin to mpd (use in tandem with jellyfin-mopidy and mpd-mopidy). Similar to ncmpcpp's "Add random" feature..

jellyshuf Essentially implements ncmpcpp's add random feature (default hotkey: `) through a script which grabs info from jellyfin api itself. jellyfin

Ethan Djeric 2 Dec 14, 2021
A Open source Discord Token Grabber with several very useful features coded in python 3.9

Kiwee-Grabber A Open source Discord Token Grabber with several very useful features coded in python 3.9 This only works on any python 3.9 versions. re

Vesper 40 Jan 01, 2023
Stock trading bot made using the Robinhood API / Python library...

High-Low Stock trading bot made using the Robinhood API / Python library... Index Installation Use Development Notes Installation To Install and run t

Reed Graff 1 Jan 07, 2022
This script will detect changes in your session using Discords built in Gateway.

Detect Session Gateway This script will detect changes in your session using Discords built in Gateway. What does this log? Discord build version Oper

Omega 5 Dec 18, 2021
Easy Google Translate: Unofficial Google Translate API

easygoogletranslate Unofficial Google Translate API. This library does not need an api key or something else to use, it's free and simple. You can eit

Ahmet Eren Odacı 9 Nov 06, 2022
A modern, easy to use, feature-rich, and async ready API wrapper for Discord written in Python.

A modern, easy to use, feature-rich, and async ready API wrapper for Discord written in Python. Key Features Modern Pythonic API using async and await

Senpai Development 4 Nov 05, 2021
Rbx-mass-send - mass sends trades to item owners

mass sends trades to item owners proxies should be in ip:port format itemsToSend

0 Feb 20, 2022
Un bot leggero basato su py-cord facile da hostare sul cloud

GalbiBot Un bot leggero basato su py-cord facile da hostare sul cloud Guida installazione su una macchina Per far funzionare il bot devi aver installa

Galbaninoh 2 Oct 21, 2022
A file-based quote bot written in Python

Let's Write a Python Quote Bot! This repository will get you started with building a quote bot in Python. It's meant to be used along with the Learnin

Jyoti prakash Rout 1 Jan 08, 2022
Anti-league-discordbot - Harrasses imbeciles for playing league of legends

anti-league-discordbot harrasses imbeciles for playing league of legends Running

Chris Clem 2 Feb 12, 2022
a small cli to generate AWS Well Architected Reports on the road

well-architected-review This repo intends to publish some scripts related to Well Architected Reviews. war.py extracts in txt & xlsx files all the WAR

4 Mar 18, 2022
SpotPlay2YouPlay - Converts new additions to a Spotify playlist to a matching Youtube playlist

SpotPlay2YouPlay - Converts new additions to a Spotify playlist to a matching Youtube playlist, can also be configured to converting whole playlists with the refresh fun

9 Mar 06, 2022