【appium实现平板QQ说说点赞】

appium实现平板QQ说说点赞

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": "14.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:
    # 等待页面加载并点击动态
    WebDriverWait(driver, 30).until(
        EC.element_to_be_clickable((By.XPATH, '//android.widget.TextView[@resource-id="com.tencent.mobileqq:id/kbi" and @text="动态"]'))
    ).click()
    print("已成功点击动态")

    # 进入空间
    WebDriverWait(driver, 30).until(
        EC.element_to_be_clickable((By.ID, "com.tencent.mobileqq:id/e6w"))
    ).click()
    print("已成功进入空间")

    # 最大尝试次数
    max_attempts = 10
    attempts = 0

    while attempts < max_attempts:
        try:
            # 尝试查找并获取点赞按钮
            like_buttons = WebDriverWait(driver, 5).until(
                EC.presence_of_all_elements_located((By.ID, "com.tencent.mobileqq.qzone_df_impl:id/ixc"))
            )

            # 遍历所有点赞按钮,检查是否未被点赞
            for like_button in like_buttons:
                is_selected = like_button.get_attribute("selected")
                print(f"点赞按钮的 selected 属性为: {is_selected}")

                # 仅在 selected 为 false 时点击
                if is_selected.lower() == "false":  # 注意: get_attribute 返回的是字符串
                    like_button.click()
                    print("点赞成功!")
                    break  # 如果成功点击,退出循环
            else:
                print("所有点赞按钮都已被选中,继续尝试...")
                driver.execute_script("mobile: dragGesture", {
                    "startX": 500,
                    "startY": 1000,
                    "endX": 500,
                    "endY": 500,
                    "duration": 1000
                })
                time.sleep(1)  # 等待一秒以便页面加载新内容
                attempts += 1
                continue

            # 如果成功点击,则退出外层循环
            break

        except Exception as e:
            print(f"未找到点赞按钮,尝试第 {attempts + 1} 次: {e}")
            # 如果未找到元素,进行滑动
            driver.execute_script("mobile: dragGesture", {
                "startX": 500,
                "startY": 1000,
                "endX": 500,
                "endY": 500,
                "duration": 1000
            })
            time.sleep(1)  # 等待一秒以便页面加载新内容
            attempts += 1

    if attempts == max_attempts:
        print("未能找到未点赞按钮,已达到最大尝试次数。")

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

finally:
    # 不关闭驱动,确保应用保持打开
    print("驱动保持打开,未关闭应用。")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值