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 appium .webdriver .common .touch_action import TouchAction
7
+ from selenium .webdriver .support import expected_conditions as EC
8
+
9
+ desired_capabilities = {
10
+ 'platformName' : 'Android' , # 操作系统
11
+ 'deviceName' : '2a254a02' , # 设备 ID
12
+ 'platformVersion' : '10.0.10' , # 设备版本号,在手机设置中查看
13
+ 'appPackage' : 'com.tencent.mm' , # app 包名
14
+ 'appActivity' : 'com.tencent.mm.ui.LauncherUI' , # app 启动时主 Activity
15
+ 'noReset' : True # 是否保留 session 信息 避免重新登录
16
+ }
17
+
18
+ # 判断元素是否存在
19
+ def is_element_exist_by_xpath (driver , text ):
20
+ try :
21
+ driver .find_element_by_xpath (text )
22
+ except Exception as e :
23
+ return False
24
+ else :
25
+ return True
26
+
27
+ if __name__ == '__main__' :
28
+ driver = webdriver .Remote ('http://localhost:4723/wd/hub' , desired_capabilities )
29
+ # 设置等待超时时间
30
+ wait = WebDriverWait (driver , 60 )
31
+
32
+ while True :
33
+ time .sleep (0.5 )
34
+
35
+ # 进入第一个聊天框
36
+ red_packet_group = driver .find_elements_by_id ('com.tencent.mm:id/e3x' )[0 ]
37
+ red_packet_group .click ()
38
+
39
+ # 检查红包
40
+ reds = driver .find_elements_by_id ('com.tencent.mm:id/r2' )
41
+ if len (reds ) == 0 :
42
+ driver .keyevent (4 )
43
+ else :
44
+ for red in reds [::- 1 ]:
45
+ red .click ()
46
+ # 领取了
47
+ is_open = is_element_exist_by_xpath (driver , '//android.widget.TextView[contains(@text, "已存入零钱")]' )
48
+ # 没抢到
49
+ is_grabbed = is_element_exist_by_xpath (driver , '//android.widget.TextView[contains(@text, "手慢了")]' )
50
+
51
+ if is_open or is_grabbed :
52
+ driver .keyevent (4 )
53
+ else :
54
+ wait .until (EC .element_to_be_clickable ((By .ID , "com.tencent.mm:id/den" ))).click ()
55
+ wait .until (EC .element_to_be_clickable ((By .ID , "com.tencent.mm:id/dm" ))).click ()
56
+
57
+ TouchAction (driver ).long_press (red ).perform ()
58
+ wait .until (EC .element_to_be_clickable ((By .ID , "com.tencent.mm:id/gam" ))).click ()
59
+ wait .until (EC .element_to_be_clickable ((By .ID , "com.tencent.mm:id/doz" ))).click ()
60
+ driver .keyevent (4 )
0 commit comments