对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
Este programa tem como objetivo o cadastro dos usuários. Assim, caso a autenticação seja feita, permitir que o usuário entre em determinado sistema ou programa.

LoginPy Este programa tem como objetivo o cadastro dos usuários. Assim, caso a autenticação seja feita, permitir que o usuário entre em determinado si

Jonas Carvalho 4 Dec 23, 2021
API which returns cusswords , can be used to check cusswords in bots etc.

Anti-abuse-api-flask API which returns cusswords , can be used to check cusswords in bots etc. Run pip install -r requirements.txt py app.py API Endpo

8 Jan 03, 2023
Telegram bot to stream videos in telegram voicechat for both groups and channels. Supports live strams, YouTube videos and telegram media.

Telegram bot to stream videos in telegram voicechat for both groups and channels. Supports live strams, YouTube videos and telegram media.

SUBIN 449 Dec 27, 2022
Purpose To make a cloudflare challenge pass successfully, Can be use cf_clearance bypassed by cloudflare

Purpose To make a cloudflare challenge pass successfully, Can be use cf_clearance bypassed by cloudflare, However, with the cf_clearance, make sure you use the same IP and UA as when you got it.

vvanglro 129 Jan 09, 2023
A bot can play all variants, but standard are abit weak, so if you need strongest you can change fsf instead of stockfish_14_Dev

MAINTAINERS Drdisrespect1 and drrespectable lichess-bot Engine communication code taken from https://github.com/ShailChoksi/lichess-bot by ShailChoksi

RPNS Nimsilu 1 Dec 12, 2021
A Twitter Bot that retweets and likes tweets with the hashtag #girlscriptwoc and #girlscript, and also follows the user.

GirlScript Winter of Contributing Twitter Bot A Twitter Bot that retweets and likes tweets with the hashtag #girlscriptwoc and #girlscript, and also f

Pranay Gupta 9 Dec 15, 2022
A MassDM selfbot which is working in 2021

mass-dm-discord - Little preview of the Logger and the Spammer Features Logging User IDS Sending DMs to the logged IDs Blacklist IDs (add the ID of th

karma.meme 88 Dec 26, 2022
RP2 is a privacy-focused, free, open-source US cryptocurrency tax calculator

Privacy-focused, free, open-source cryptocurrency US tax calculator, up to date for 2021: it handles multiple coins/exchanges and computes long/short-term capital gains, cost bases, in/out lot relati

eprbell 123 Jan 04, 2023
This bot will pull a stream of tweets based on rules you set and automatically reply to them.

Twitter reply bot This bot will pull a stream of tweets based on rules you set and automatically reply to them. I built this bot in order to help comb

Brains 1 Feb 13, 2022
Python bindings for BigML.io

BigML Python Bindings BigML makes machine learning easy by taking care of the details required to add data-driven decisions and predictive power to yo

BigML Inc, Machine Learning made easy 271 Dec 27, 2022
Demo to explain how to use AWS Chalice to connect to twitter API and change profile picture at scheduled times.

chalice-twitter-demo Demo to explain how to use AWS Chalice to connect to twitter API and change profile picture at scheduled times. Video Demo Click

Ahmed Mohamed 4 Dec 13, 2021
A discord bot written in discord.py to manage custom roles assigned to boosters of your server.

BBotty A discord bot written in discord.py to manage custom roles assigned to boosters of your server. v0.0.1-alpha released! This version is incomple

Oui002 1 Nov 27, 2021
Optimus Prime - A modular Telegram group management and drive clone bot running on Python with sqlalchemy database

Optimus Prime Bot . 🤖 A modular Telegram group management and drive clone bot r

9 Jun 01, 2022
A hyper-user friendly bot framework built on hikari

Framework A hyper-user friendly bot framework built on hikari. Framework is based off the blocking discord library disco, In both modularity and struc

Vincent 1 Jan 10, 2022
The programm for collecting data from Tinkoff API and building Excel table.

tinkproject The program for portfolio analysis via Tinkoff API Hello! This is my first project, please, don't judge me. This project was developed for

214 Dec 02, 2022
Collection of AWS Fault Injection Simulator (FIS) experiment templates.

Collection of AWS Fault Injection Simulator (FIS) experiment templates. These templates let you perform chaos engineering experiments on resources (applications, network, and infrastructure) in the A

Adrian Hornsby 8 Nov 27, 2022
Simple spam bot made in python

Simple Spam Bot A Simple and easy way to be the most hated person between your friends, All you have to do is spam the group chat using this bot until

Kareem Osama 6 Sep 05, 2022
Telegram Google Translater Bot Can Translate Any Language To Your Selected Language

🔰 TELEGRAM GOOGLE TRANSLATER 🔰 • ⚡ INSTALLING ⚡ • • ✅ OFFICIAL SUPPORTS ✅ •

⚝ANKIT KUMAR⚝ 2 Jan 16, 2022
A discord tool to use bugs and exploits

DiscordTool A discord tool to use bugs and exploits Features: send a buggy messa

6 Aug 19, 2022