// Auto call WeChat contact named "zxxs"
// Make sure WeChat is installed and logged in
auto.waitFor(); // Wait for accessibility service
launchApp("WeChat");
sleep(3000); // Wait for WeChat to launch
// Tap on the search icon (top right)
var searchIcon = desc("Search").findOne(5000) || desc("搜索").findOne(5000);
if (!searchIcon) {
toast("Search icon not found");
exit();
}
searchIcon.click();
sleep(1000);
// Type contact name
setText("zxxs");
sleep(2000);
// Select the contact from the search results
var contact = text("zxxs").findOne(5000);
if (!contact) {
toast("Contact zxxs not found");
exit();
}
contact.click();
sleep(2000);
// Tap on the top-right "..." (menu)
var menuBtn = desc("More").findOne(5000) || desc("更多").findOne(5000);
if (!menuBtn) {
toast("Menu not found");
exit();
}
menuBtn.click();
sleep(1000);
// Tap on "Voice Call" (语音通话)
var voiceCall = text("Voice Call").findOne(3000) || text("语音通话").findOne(3000);
if (!voiceCall) {
toast("Voice Call button not found");
exit();
}
voiceCall.click();
toast("Calling zxxs...");
有错误吗 我想打开微信 搜索 zxxs 然后打电话
回答
你的脚本整体结构是正确的,但存在几个潜在问题需要修正才能正常工作。以下是优化后的版本和说明:
// 文档地址 https://docs.hamibot.com/
auto.waitFor(); // 确保已开启无障碍服务
launchApp("WeChat");
sleep(3000);
// 查找并点击搜索图标
var searchIcon = desc("Search").findOne(5000) || desc("搜索").findOne(5000);
if (!searchIcon) {
toast("未找到搜索图标");
exit();
}
searchIcon.click();
sleep(1000);
// 定位搜索输入框并输入联系人名称
var input = className("EditText").findOne(3000); // 通过控件类型定位
if (!input) {
toast("未找到搜索框");
exit();
}
input.setText("zxxs"); // 正确的 setText 使用方式
sleep(2000);
// 查找搜索结果中的联系人
var contact = text("zxxs").findOne(5000);
if (!contact) {
toast("未找到联系人");
exit();
}
contact.click();
sleep(2000);
// 查找并点击更多按钮
var menuBtn = desc("More").findOne(5000) || desc("更多").findOne(5000);
if (!menuBtn) {
toast("未找到更多按钮");
exit();
}
menuBtn.click();
sleep(1000);
// 查找并点击语音通话
var voiceCall = text("语音通话").findOne(3000) || text("Voice Call").findOne(3000);
if (!voiceCall) {
toast("未找到语音通话");
exit();
}
voiceCall.click();
toast("正在拨打zxxs...");
主要修改说明:
setText("zxxs")
需要配合className("EditText").findOne()
使用才能正确定位输入框- 增加了更可靠的控件类型定位(className)
- 优化了多语言支持(同时检查中英文文本)
- 添加了更详细的错误提示
- 保持了原有的核心逻辑流程
建议在实际使用前:
- 确保已授予Hamibot所有必要权限
- 根据实际微信版本调整控件描述符
- 可在关键步骤增加
dumpHierarchy()
查看界面结构进行调试 - 注意微信的反自动化机制可能需要额外处理