Airtest遇到模拟器无法输入中文的情况该如何处理?

此文章来源于项目官方公众号:"AirtestProject"

版权声明:允许转载,但转载必须保留原链接;请勿用作商业或者非法用途

1. 前言

最近有收到同学们的一些提问,使用Airtest的 text 接口,发现在部分模拟器上, text 无法输入中文,不知道该怎么处理。

今天我们就输入这个小问题,来详细聊一下。

2. Airtest的输入法简介

对于Android设备来说,Airtest的输入功能,由 Yosemite.apk 提供。有了这个apk,就可以支持我们在Android设备上输入中文,而不是局限于只能输入英文字母的 adb input text

所以在调用Airtest的 text 接口之前,请务必在Android设备上安装好 Yosemite.apk

3.在部分特殊设备上无法输入中文

也有一些情况,即使我们安装好了 Yosemite.apk ,使用 text 接口进行输入时,却只能输入英文,没法输入中文,我们以mumu12模拟器为例:

在给mumu12输入中文的时候,出现了1个报错:

swift 复制代码
airtest.core.error.AdbShellError: stdout[b''] stderr[b"\r\nException
occurred while executing 'text':\r\njava.lang.NullPointerException:
Attempt to get length of null array\r\n\tatcom.android.server.input.InputShellCommand.sendText(InputShellCommand.java:284)\r\n\tat 
com.android.server.input.InputShellCommand.runText(InputShellCommand.java:256)\r\n\tat
com.android.server.input.InputShellCommand.onCommand(InputShellCommand.java:197)\r\n\tat com.android.modules.utils.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:97)\r\n\tat
android.os.ShellCommand.exec(ShellCommand.java:38)\r\n\tat com.android.server.input.InputManagerService.onShellCommand(InputManagerService.java:3591)\r\n\tat
android.os.Binder.shellCommand(Binder.java:950)\r\n\tat
android.os.Binder.onTransact(Binder.java:834)\r\n\tat
android.hardware.input.IInputManager$Stub.onTransact(IInputManager.java:1167)\r\n\tat android.os.Binder.execTransactInternal(Binder.java:1184)\r\n\tat android.os.Binder.execTransact(Binder.java:1143)\r\n"]

这是因为模拟器没有真正把 Yosemite 输入法调起来导致的,我们可以到 Yosemite.apk 里面,找到输入法,启用 Yosemite 输入法,之后关闭掉其余的输入法:

之后再通过下述脚本的方式,给设备输入中文文本:

python 复制代码
# -*- encoding=utf8 -*-
__author__ = "AirtestProject"
​
from airtest.core.api import *
auto_setup(__file__)
​
shell('am broadcast -a INPUT_TEXT --es text "大话西游"')

4. 小结

今天主要讲述了,在部分特殊Android设备里,遇到 text 输入接口无法完成的一些输入事情的时候,我们还可以通过怎样的方式来实现我们的输入效果,本质还是调用了 Yosemite.apk 里面的功能。

如果还有其他关于输入感兴趣的问题,欢迎给我们私信/留言。


AirtestIDE下载airtest.netease.com/
Airtest 教程官网airtest.doc.io.netease.com/
搭建企业私有云服务airlab.163.com/b2b

官方答疑 Q 群:117973773

相关推荐
晚烛1 分钟前
CANN 模型蒸馏实战:大模型知识迁移到小模型
python·线性代数·矩阵
俊哥工具2 分钟前
解决网速卡顿、断网、网络报错,万能网络修复工具教程
网络·python·django·计算机外设·智能路由器·pygame
WL_Aurora4 分钟前
Python爬虫实战(九):百度百聘招聘数据采集
爬虫·python·百度
lili00124 分钟前
Gemini 3.5发布后的AI格局:谷歌重新定义行业标准
java·人工智能·python·ai编程
JunLa8 分钟前
Java语法糖
java·python·哈希算法
财经资讯数据_灵砚智能10 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年5月21日
大数据·人工智能·python·信息可视化·自然语言处理
水木流年追梦13 分钟前
大模型入门-RL基础
开发语言·python·算法·leetcode·正则表达式
Cthy_hy15 分钟前
基于首届中国互联网数据挖掘竞赛数据集的行为相似网络分析
python·信息可视化·数据挖掘
AI玫瑰助手19 分钟前
Python运算符:逻辑运算符(and/or/not)的短路特性
开发语言·python·信息可视化
是梦终空19 分钟前
计算机源码274—基于深度学习的中医舌象智能识别与健康管理系统(源代码+数据库+12000字论文)
人工智能·python·深度学习·opencv·django·vue·springboot