|
| 1 | +import time |
| 2 | + |
| 3 | +from appium import webdriver |
| 4 | +from selenium.webdriver.common.by import By |
| 5 | +from selenium.webdriver.support.ui import WebDriverWait |
| 6 | +from selenium.webdriver.support import expected_conditions as EC |
| 7 | + |
| 8 | +from appium.webdriver.common.touch_action import TouchAction |
| 9 | + |
| 10 | +desired_capabilities = { |
| 11 | + 'platformName': 'Android', # 操作系统 |
| 12 | + 'deviceName': '2a254a02', # 设备 ID |
| 13 | + 'platformVersion': '10.0.10', # 设备版本号,在手机设置中查看 |
| 14 | + 'appPackage': 'com.eg.android.AlipayGphone', # app 包名 |
| 15 | + 'appActivity': 'AlipayLogin', # app 启动时主 Activity |
| 16 | + 'noReset': True # 是否保留 session 信息 避免重新登录 |
| 17 | + } |
| 18 | + |
| 19 | +# 判断元素是否存在 |
| 20 | +def is_element_exist_by_xpath(driver, text): |
| 21 | + try: |
| 22 | + driver.find_element_by_xpath(text) |
| 23 | + except Exception as e: |
| 24 | + return False |
| 25 | + else: |
| 26 | + return True |
| 27 | + |
| 28 | +# 收取能量 |
| 29 | +def collect_energy(driver, width, height): |
| 30 | + # 能量球可能出现的区域坐标 |
| 31 | + start_x = 150 |
| 32 | + end_x = 900 |
| 33 | + start_y = 540 |
| 34 | + end_y = 900 |
| 35 | + |
| 36 | + for x in range(start_x, end_x, 50): |
| 37 | + for y in range(start_y, end_y, 50): |
| 38 | + x_scale = int((int(x) / width) * width) |
| 39 | + y_scale = int((int(y) / height) * height) |
| 40 | + # 点击指定坐标 |
| 41 | + TouchAction(driver).press(x=x_scale, y=y_scale).release().perform() |
| 42 | + print('能量收取完毕') |
| 43 | + |
| 44 | +def search_energy(driver, width, height): |
| 45 | + |
| 46 | + x = int((int(1000) / width) * width) |
| 47 | + y = int((int(1550) / height) * height) |
| 48 | + # 点击指定坐标 |
| 49 | + TouchAction(driver).press(x=x, y=y).release().perform() |
| 50 | + time.sleep(1) |
| 51 | + is_collected = is_element_exist_by_xpath(driver, '//android.widget.Button[contains(@text, "返回我的森林")]') |
| 52 | + if is_collected: |
| 53 | + print('能量全部收集完毕') |
| 54 | + return |
| 55 | + |
| 56 | + collect_energy(driver, width, height) |
| 57 | + search_energy(driver, width, height) |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == '__main__': |
| 62 | + |
| 63 | + driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_capabilities) |
| 64 | + print('支付宝启动') |
| 65 | + # 设置等待超时时间 |
| 66 | + wait = WebDriverWait(driver, 60) |
| 67 | + |
| 68 | + wait.until(EC.element_to_be_clickable((By.ID, 'com.alipay.android.phone.openplatform:id/more_app_icon'))).click() |
| 69 | + wait.until(EC.element_to_be_clickable((By.XPATH, '//android.widget.TextView[contains(@text, "蚂蚁森林")]'))).click() |
| 70 | + time.sleep(3) |
| 71 | + # 获取手机屏幕宽高 |
| 72 | + width = int(driver.get_window_size()['width']) |
| 73 | + height = int(driver.get_window_size()['height']) |
| 74 | + |
| 75 | + collect_energy(driver, width, height) |
| 76 | + |
| 77 | + search_energy(driver, width, height) |
0 commit comments