appium+python3.X 自动模拟点击APP双12活动
- # 自动化淘宝活动:欢乐币
- import time
- from appium import webdriver
- from selenium.webdriver.support.ui import WebDriverWait
-
-
- def main():
- desired_caps = {
- 'platformName': 'Android', # 被测手机是安卓
- 'platformVersion': '7', # 手机安卓版本
- 'deviceName': 'xxx', # 设备名,安卓手机可以随意填写
- 'appPackage': 'com.taobao.taobao', # 启动APP Package名称 com.jingdong.app.mall com.ss.android.ugc.aweme.lite
- 'appActivity': 'com.taobao.tao.welcome.Welcome',
- # 启动Activity名称 .main.MainActivity com.ss.android.ugc.aweme.main.MainActivity
- 'unicodeKeyboard': True, # 使用自带输入法,输入中文时填True
- 'resetKeyboard': True, # 执行完程序恢复原来输入法
- 'noReset': True, # 不要重置App
- 'newCommandTimeout': 6000,
- 'automationName': 'UiAutomator2'
- # 'app': r'd:\apk\bili.apk',
- }
-
- # 连接Appium Server,初始化自动化环境
- driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
-
- # 设置缺省等待时间
- driver.implicitly_wait(3)
-
- # 如果有`青少年保护`界面,点击`我知道了`
- # iknow = driver.find_elements_by_id("text3")
- # if iknow:
- # iknow.click()
-
- while True:
- try:
- print("定位搜索框")
- # 通过xpth定位搜索框
- search_str = '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.View'
- driver.find_element_by_xpath(search_str).click()
- time.sleep(1.5)
- # 向输入框中发送文字
- driver.find_element_by_id("com.taobao.taobao:id/searchEdit").send_keys('欢乐造红包')
- time.sleep(1)
- driver.find_element_by_id("com.taobao.taobao:id/searchbtn").click()
- break
- except:
- time.sleep(1)
- driver.back()
-
- time.sleep(9)
- # ss = 'com.taobao.taobao:id/decor_content_parent'
- # ss = 'android:id/content'
- # WebDriverWait(driver, 10).until(lambda x: x.find_element_by_accessibility_id(ss).is_displayed(),message='not find')
- # driver.tap([(939,1855)])
- return driver
-
-
- def broswer(driver):
- while True:
- try:
- time.sleep(1.5)
- driver.find_element_by_android_uiautomator('new UiSelector().text("领欢乐币")').click()
- print("找到领欢乐币文字...")
- break
- except:
- print("未找到领欢乐币文字...")
- time.sleep(2)
- while True:
- try:
- time.sleep(2)
- # 获取次数 浏览adidas旗舰店(10/15)
- # count_str = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.widget.ListView/android.view.View[3]/android.view.View/android.view.View[1]'
- # count_info = driver.find_element_by_xpath(count_str).text
- # 获取所有标签
- count_strs = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.widget.ListView/android.view.View/android.view.View/android.view.View[1]'
- count_infos = driver.find_elements_by_xpath(count_strs)
- broswer_ix = -1
- for broswer_ix, val in enumerate(count_infos):
- val_text = val.text
- if val_text.startswith('浏览'):
- print("*val_text*", val_text)
- broswer_ix = broswer_ix
- break
- elif val_text.startswith('观看'):
- print("浏览观看开头,重新进")
- # 点×再重新进
- cha = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.Button'
- driver.find_element_by_xpath(cha).click()
- time.sleep(1)
- driver.find_element_by_android_uiautomator('new UiSelector().text("领欢乐币")').click()
- time.sleep(3)
- continue
- if broswer_ix != -1:
- count_str = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.widget.ListView/android.view.View[' + str(
- broswer_ix) + ']/android.view.View/android.view.View[1]'
- count_info = driver.find_element_by_xpath(count_str).text
- count = (count_info.split('(')[-1]).split('/')[0]
- count_all = (count_info.split('(')[-1]).split('/')[-1][:-1]
- if int(count) < int(count_all):
- time.sleep(1)
- # 点击去完成
- no_complete = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.widget.ListView/android.view.View[' + str(
- broswer_ix) + ']/android.widget.Button'
- driver.find_element_by_xpath(no_complete).click()
- time.sleep(16.5)
- while True:
- try:
- # 浏览完15秒显示任务完成后返回
- task = '(//com.uc.webview.export.WebView[@content-desc="WVUCWebView"])[1]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[1]/android.view.View'
- task_txt = driver.find_element_by_xpath(task).text
- if task_txt == '任务完成' or task_txt == '任务已经全部完成啦':
- print(task_txt)
- time.sleep(1)
- driver.back()
- time.sleep(1)
- break
- except:
- time.sleep(1)
- driver.back()
- time.sleep(1)
- break
- elif int(count) < int(count_all) and count_info.startswith('观看'):
- # 点×再重新进
- cha = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.Button'
- driver.find_element_by_xpath(cha).click()
- time.sleep(1)
- driver.find_element_by_android_uiautomator('new UiSelector().text("领欢乐币")').click()
- time.sleep(2)
- continue
- else:
- print("无需处理浏览:", count_info)
- break
- else:
- break
- except Exception as e:
- print("浏览出错了,继续", e)
- continue
-
-
- def gg(driver):
- time.sleep(2)
- print("准备处理逛一逛")
- while True:
- try:
- time.sleep(1)
- # 获取次数 浏览adidas旗舰店(10/15)
- # count_str = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.widget.ListView/android.view.View[4]/android.view.View/android.view.View[1]'
- # count_info = driver.find_element_by_xpath(count_str).text
- # 获取所有标签
- count_strs = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.widget.ListView/android.view.View/android.view.View/android.view.View[1]'
- count_infos = driver.find_elements_by_xpath(count_strs)
- for go_ix, val in enumerate(count_infos):
- val_text = val.text
- if val_text.startswith('逛一逛'):
- print("*val_text*", val_text)
- go_ix = go_ix
- break
- elif val_text.startswith('观看'):
- print("逛一逛观看开头,重新进")
- # 点×再重新进
- cha = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.Button'
- driver.find_element_by_xpath(cha).click()
- time.sleep(1)
- driver.find_element_by_android_uiautomator('new UiSelector().text("领欢乐币")').click()
- time.sleep(2)
- continue
- else:
- go_ix = -1
- print("go_ix", go_ix)
- if go_ix != -1:
- count_str = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.widget.ListView/android.view.View[' + str(
- go_ix) + ']/android.view.View/android.view.View[1]'
- count_info = driver.find_element_by_xpath(count_str).text
- print(count_info)
- count = (count_info.split('(')[-1]).split('/')[0]
- count_all = (count_info.split('(')[-1]).split('/')[-1][:-1]
- if int(count) < int(count_all):
- time.sleep(1)
- # 点击去完成
- no_complete = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.widget.ListView/android.view.View[' + str(
- go_ix) + ']/android.widget.Button'
- driver.find_element_by_xpath(no_complete).click()
- time.sleep(16.5)
- # 可以注释掉,然后直接返回
- while True:
- try:
- print("进入任务页面,等待完成。。。")
- # 浏览完15秒显示任务完成后返回
- task = '(//com.uc.webview.export.WebView[@content-desc="WVUCWebView"])[1]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[1]/android.view.View'
- task_txt = driver.find_element_by_xpath(task).text
- print(task,task_txt)
- if task_txt == '任务完成' or task_txt == '任务已经全部完成啦':
- print(task_txt)
- time.sleep(1)
- driver.back()
- time.sleep(1)
- break
- # 有时候task_txt等于:关注
- else:
- time.sleep(2)
- driver.back()
- break
- except Exception as e:
- print("等待任务页面出错,",e)
- time.sleep(2)
- driver.back()
- time.sleep(1)
- break
- elif int(count) < int(count_all) and count_info.startswith('观看'):
- # 点×再重新进
- cha = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.Button'
- driver.find_element_by_xpath(cha).click()
- time.sleep(1)
- driver.find_element_by_android_uiautomator('new UiSelector().text("领欢乐币")').click()
- time.sleep(2)
- continue
- else:
- print("无需处理逛一逛:", count_info)
- break
- else:
- break
- except Exception as e:
- print("逛一逛出错了,继续", e)
- continue
-
-
- def so_so(driver):
- time.sleep(2)
- print("准备处理搜一搜")
- while True:
- try:
- time.sleep(1)
- # 获取次数 浏览adidas旗舰店(10/15)
- # count_str = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.widget.ListView/android.view.View[4]/android.view.View/android.view.View[1]'
- # count_info = driver.find_element_by_xpath(count_str).text
- # 获取所有标签
- count_strs = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.widget.ListView/android.view.View/android.view.View/android.view.View[1]'
- count_infos = driver.find_elements_by_xpath(count_strs)
- for so_ix, val in enumerate(count_infos):
- val_text = val.text
- if val_text.startswith('搜一搜'):
- print("*val_text*", val_text)
- so_ix = so_ix
- break
- else:
- so_ix = -1
- print("so_ix:", so_ix)
- if so_ix != -1:
- count_str = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.widget.ListView/android.view.View[' + str(
- so_ix) + ']/android.view.View/android.view.View[1]'
- count_info = driver.find_element_by_xpath(count_str).text
- count = (count_info.split('(')[-1]).split('/')[0]
- count_all = (count_info.split('(')[-1]).split('/')[-1][:-1]
- if int(count) < int(count_all):
- time.sleep(1)
- # 点击去完成
- no_complete = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.widget.ListView/android.view.View[' + str(
- so_ix) + ']/android.widget.Button'
- driver.find_element_by_xpath(no_complete).click()
- time.sleep(16.5)
- while True:
- try:
- # 浏览完15秒显示任务完成后返回
- task = '(//com.uc.webview.export.WebView[@content-desc="WVUCWebView"])[1]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[1]/android.view.View'
- task_txt = driver.find_element_by_xpath(task).text
- if task_txt == '任务完成' or task_txt == '任务已经全部完成啦':
- print(task_txt)
- time.sleep(1)
- driver.back()
- time.sleep(1)
- break
- except:
- time.sleep(2)
- driver.back()
- time.sleep(1)
- break
- elif int(count) < int(count_all) and count_info.startswith('观看'):
- # 点×再重新进
- cha = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.Button'
- driver.find_element_by_xpath(cha).click()
- time.sleep(1)
- driver.find_element_by_android_uiautomator('new UiSelector().text("领欢乐币")').click()
- time.sleep(2)
- continue
- else:
- print("无需处理搜一搜:", count_info)
- break
- else:
- print("搜一搜处理完成,退出循环")
- break
- except Exception as e:
- print("搜一搜出错了,继续", e)
- continue
-
- try:
- cha = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.Button'
- driver.find_element_by_xpath(cha).click()
- except Exception as e:
- print("尝试点×出错", e)
- pass
-
-
- def browser_gold(driver):
- # 获取屏幕宽高:
- width = driver.get_window_size()['width'] # 1080
- height = driver.get_window_size()['height'] # 1920
- time.sleep(2)
- # 寻找逛一逛金币页面
- print("准备处理逛金币,屏幕尺寸宽和高分别为:", width, height)
- jinbi = False
- while True:
- try:
- time.sleep(1.5)
- driver.find_element_by_android_uiautomator('new UiSelector().text("逛淘金币小镇 购物可用金币抵")').click()
- print("找到逛淘金币小镇 购物可用金币抵文字...")
- time.sleep(1)
- break
- except Exception as e:
- print("未找到逛淘金币小镇 购物可用金币抵文字...重新获取", e)
- time.sleep(2)
- while True:
- try:
- time.sleep(5)
- print("点击赚金币")
- driver.find_element_by_android_uiautomator('new UiSelector().text("赚金币")').click()
- time.sleep(1)
- jinbi = True
- break
- except Exception as e:
- print("点击赚金币未获取到,重新获取", e)
- time.sleep(2)
- LI = ['逛猜你喜欢', '逛好店领', '逛聚划算双', '逛淘金币首页', '逛淘金币抵扣', '签到领话费','好店浏览得', '逛逛大牌','看免费小',
- '逛高比例抵扣', '逛淘金币专属', '浏览福利', '逛淘金币超值', '逛优质抵扣', '欢乐造12', '逛健康大', '天猫国际', '逛苏宁']
- while True:
- try:
- if jinbi and LI:
- # 领取奖励按钮
- try:
- rewards = driver.find_elements_by_android_uiautomator('new UiSelector().text("领取奖励")')
- length = len(rewards)
- print("可领奖励个数:", length)
- for _ in range(length):
- time.sleep(2)
- reward_ = driver.find_element_by_android_uiautomator('new UiSelector().text("领取奖励")')
- time.sleep(1)
- print("点击领取奖励")
- reward_.click()
- time.sleep(2)
- except:
- pass
- time.sleep(1)
- # 获取所有标签
- count_strs = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[7]/android.view.View[2]/android.widget.ListView/android.widget.ListView/android.view.View/android.view.View/android.view.View[1]'
- count_infos = driver.find_elements_by_xpath(count_strs)
- # 查看按钮上的文字是已完成还是去完成
- button_t = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[7]/android.view.View[2]/android.widget.ListView/android.widget.ListView/android.view.View/android.widget.Button'
- button_txts = driver.find_elements_by_xpath(button_t)
- for i in count_infos:
- print("***", i.text)
- # 构造字典,key是标签名,value是未完成,已完成的不添加
- item = {}
- for i in range(len(count_infos)):
- # 可以在这里处理已完成的:逛高比例抵扣商品(1/1)
- if button_txts[i].text != '已完成':
- item[count_infos[i].text] = button_txts[i].text
- # 终止外循环
- break_out = False
- # 如果有每日来访领能量这个标签,则so_ix要+1
- coll_engine = False
- # print(item)
- so_ix = -1
- li = ''
- for so_ix, val in enumerate(item):
- val_text = list(item.keys())[so_ix]
- print("----", so_ix, val_text, item[val])
- if val_text.startswith('每日来访领能量'):
- coll_engine = True
- for li in LI:
- if val_text.startswith(li):
- print("*val_text*", val_text, so_ix)
- break_out = True
- so_ix = so_ix
- li = li
- break
- if break_out:
- break
- if not break_out:
- LI.remove(li)
- print("没有匹配到需要处理的数据,退出")
- break
- print("so_ix:", so_ix, li)
- # if coll_engine:
- # so_ix = so_ix + 1
- so_ix = so_ix + 1
- print("so_ix:", so_ix)
- if so_ix != -1:
- slide_up = 0
- count_str = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[7]/android.view.View[2]/android.widget.ListView/android.widget.ListView/android.view.View[' + str(
- so_ix) + ']/android.view.View/android.view.View[1]'
- count_info = driver.find_element_by_xpath(count_str).text
- count = (count_info.split('(')[-1]).split('/')[0]
- count_all = (count_info.split('(')[-1]).split('/')[-1][:-1]
- print("count", count, count_all, count_info)
- if so_ix == 8 or so_ix == 7:
- # 向上滑动,y坐标值由大到小
- print("向上滑动1次")
- slide_up = 1
- elif so_ix == 9 or so_ix == 10:
- print("向上滑动2次")
- slide_up = 2
- # 向上滑动,y坐标值由大到小
- elif so_ix == 11 or so_ix == 12:
- print("向上滑动3次")
- slide_up = 3
- elif so_ix == 13 or so_ix == 14:
- print("向上滑动4次")
- slide_up = 4
- elif so_ix == 15 or so_ix == 16:
- print("向上滑动5次")
- slide_up = 5
- if slide_up > 0:
- for i in range(slide_up):
- time.sleep(1)
- driver.swipe(width / 2, height * 0.8, width / 2, height * 0.6) # 每次滑动屏幕2个位置
- time.sleep(1)
- if int(count) < int(count_all):
- time.sleep(1)
- # no_complete = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[7]/android.view.View[2]/android.widget.ListView/android.widget.ListView/android.view.View['+str(so_ix)+']/android.widget.Button'
- time.sleep(1)
- count_str_ = '//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[7]/android.view.View[2]/android.widget.ListView/android.widget.ListView/android.view.View[' + str(
- so_ix) + ']/android.view.View/android.view.View[1]'
- count_info_ = driver.find_element_by_xpath(count_str_).text
- print(count_info_, 'new UiSelector().text("' + count_info_ + '")')
- # 点击要处理的那一行文字
- driver.find_element_by_android_uiautomator(
- 'new UiSelector().text("' + count_info_ + '")').click()
- # driver.find_element_by_xpath(no_complete).click()
- time.sleep(15)
- if count_info.startswith("逛淘金币首页"):
- time.sleep(3.5)
- driver.back()
- time.sleep(2)
- if count_info.startswith("欢乐造12"):
- driver.find_element_by_android_uiautomator('new UiSelector().text("残忍离开")').click()
- time.sleep(2)
-
- if slide_up > 0:
- for i in range(slide_up + 1):
- # 向下滑动,y坐标值由小到大
- print(f"向下滑动第{i + 1}次,为了使领取奖励显示出来")
- driver.swipe(width / 2, height * 0.6, width / 2, height * 0.8) # 每次滑动屏幕2个位置
- time.sleep(2)
- # 点击领取奖励
- try:
- driver.find_element_by_android_uiautomator('new UiSelector().text("领取奖励")').click()
- except:
- pass
- else:
- print("无需处理逛金币:", count_info)
- if slide_up > 0:
- for i in range(slide_up):
- # 向下滑动,y坐标值由小到大,因为上面已经滑动过,还需滑动回去
- print(f"向下滑动第{i + 1}次,为了滑动到顶部")
- time.sleep(1)
- driver.swipe(width / 2, height * 0.6, width / 2, height * 0.8) # 每次滑动屏幕2个位置
- time.sleep(1)
- LI.remove(li)
- print("LI:", LI)
- continue
- else:
- print("逛金币处理完成,退出循环")
- break
-
- except Exception as e:
- print("逛金币出错了,继续", e)
- err = str(e)
- if "socket hang up" in err:
- print("app异常闪退,直接退出")
- break
- time.sleep(1)
- driver.back()
- time.sleep(1)
- continue
-
-
- if __name__ == '__main__':
- driver = main()
-
- broswer(driver) # 浏览
- gg(driver) # 逛一逛
- so_so(driver) # 搜一搜
-
- # 赚金币
- browser_gold(driver)
-
- input('以上处理完成 Press Enter to quit..')
- driver.quit()