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_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
}
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}")
if is_selected.lower() == "false":
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("驱动保持打开,未关闭应用。")