Bootstrap SomeTools Icon
返回
import time
from PIL import Image
from appium import webdriver
from appium.options.common.base import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
import os
from cnocr import CnOcr

# 配置 Appium 选项
options = AppiumOptions()
options.load_capabilities({
    "platformName": "Android",
    "appium:platformVersion": "14",
    "appium:deviceName": "MAA_AN10",
    "appium:appPackage": "com.tencent.mobileqq",
    "appium:appActivity": "com.tencent.mobileqq.activity.SplashActivity",
    "appium:resetKeyboard": True,
    "appium:ensureWebviewsHavePages": True,
    "appium:nativeWebScreenshot": True,
    "appium:newCommandTimeout": 3600,
    "appium:connectHardwareKeyboard": True,
    "noReset": True,  # 不重置应用
    "fullReset": False,  # 不重置缓存
    "dontStopAppOnReset": True,  # 应用不关闭
    "unicodeKeyboard": True  # 支持中文输入
})
# 连接到 Appium 服务器
print("启动中......")
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", options=options)


# 通用触摸事件函数
def tap_screen(x, y, pause_time=0.2):
    actions = ActionChains(driver)
    actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
    actions.w3c_actions.pointer_action.move_to_location(x, y)
    actions.w3c_actions.pointer_action.pointer_down()
    actions.w3c_actions.pointer_action.pause(0.1)
    actions.w3c_actions.pointer_action.release()
    actions.perform()
    time.sleep(pause_time)


# 截图获取联系人坐标
def crap_image():
    print("图片处理中......")
    select_name = "Nic"
    screenshot_path = "img/test001.png"
    driver.get_screenshot_as_file(screenshot_path)
    image = Image.open('img/test001.png')
    # 裁剪区域
    crop_area = (0, 603, 1211, 2526)
    cropped_image = image.crop(crop_area)
    print("ocr识别中......")
    # OCR识别获取数据
    ocr = CnOcr()  # 使用默认OCR设置
    out = ocr.ocr(cropped_image)
    print("位置计算中......")
    for text_cl in out:
        if select_name in text_cl["text"]:
            point_data = text_cl["position"]
    # 获取文字的中心坐标(相对于截取后的图片)
    x_point = (point_data[0][0] + point_data[2][0]) // 2
    y_point = (point_data[0][1] + point_data[2][1]) // 2

    # 缩放回原图中的坐标
    x_abs = int(x_point + crop_area[0])
    y_abs = int(y_point + crop_area[1])
    return x_abs, y_abs


# 实例操作(这里是打开app的某个见面)
def perform_actions():
    x_point, y_point = crap_image()
    print("操作运行中......")
    # 第一次触摸事件
    time.sleep(2)
    tap_screen(x_point, y_point)
    # 第二次触摸事件
    tap_screen(1013, 2632)


# 页面上的实际操作
def message():
    # 触摸消息界面
    tap_screen(93, 1802)
    # 触摸发送按钮
    tap_screen(1091, 1454)


try:
    # 先打开应用某一页
    perform_actions()
    for _ in range(2):
        message()
except Exception as e:
    print(e)
finally:
    # 退出驱动
    driver.quit()