【appium 安卓10 QQ发送消息】

代码

python 复制代码
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# 设置 Desired Capabilities
desired_caps = {
    "platformName": "Android",
    "platformVersion": "10.0",
    "deviceName": "Android Emulator",
    "appPackage": "com.tencent.mobileqq",
    "appActivity": ".activity.SplashActivity",
    "automationName": "UiAutomator2",
    "noReset": True,
    "appium:ignoreHiddenApiPolicyError": True,
    "appium:forceAppLaunch": True
}

# 创建 Appium WebDriver 实例
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

try:
    # 等待并点击搜索输入框
    search_box = WebDriverWait(driver, 30).until(
        EC.element_to_be_clickable((By.ID, "com.tencent.mobileqq:id/et_search_keyword"))
    )
    search_box.click()
    print("已成功点击搜索输入框")

     # 等待搜索框重新变得可输入
    search_box = WebDriverWait(driver, 30).until(
        EC.element_to_be_clickable((By.ID, "com.tencent.mobileqq:id/et_search_keyword"))
    )

    # 输入 "你夏哥" 到搜索框
    search_box.send_keys("你夏哥")
    print("已成功输入 '你夏哥'")

    # 等待并点击 ID 为 com.tencent.mobileqq:id/dpr 的元素
    target_element = WebDriverWait(driver, 30).until(
        EC.element_to_be_clickable((By.ID, "com.tencent.mobileqq:id/dpr"))
    )
    target_element.click()
    print("已成功点击你夏哥")

    # 等待并点击消息发送框
    input_box = WebDriverWait(driver, 30).until(
        EC.element_to_be_clickable((By.ID, "com.tencent.mobileqq:id/input"))
    )
    input_box.click()
    print("已成功点击消息发送框")

    # 输入消息 "Hello World!"
    input_box.send_keys("Hello World!")
    print("已成功输入消息 'Hello World!'")

    # 等待并点击发送按钮
    send_button = WebDriverWait(driver, 30).until(
        EC.element_to_be_clickable((By.ID, "com.tencent.mobileqq:id/fun_btn"))
    )
    send_button.click()
    print("已成功点击发送按钮")

except Exception as e:
    print(f"操作失败: {e}")

finally:
    # 不关闭驱动,确保应用保持打开
    pass
相关推荐
火柴就是我6 分钟前
每日见闻之Container Decoration
android·flutter
天枢破军7 分钟前
【AOSP】解决repo拉取提示无法连接android.googlesource.com
android
whysqwhw22 分钟前
OkHttp之AndroidPlatform类分析
android
XiaolongTu26 分钟前
Kotlin Flow详述:从一个“卡顿”问题到线程切换的本质
android·面试
Kapaseker28 分钟前
全网最详细的Compose Stable讲解,你一定要看
android
solo_9929 分钟前
使用Android Studio 聊微信
android
whysqwhw42 分钟前
OkHttp PublicSuffix包的平台化设计分析
android
whysqwhw42 分钟前
Conscrypt 源码分析全图解(附精要讲解)
android
一只柠檬新1 小时前
Kotlin object单例到底是懒汉式还是饿汉式
android·kotlin
wkj0012 小时前
php中array($this, ‘loadClass‘)表示啥意思?
android·开发语言·php