def openFriendsList(): log.logger.info('查找好友列表') sleep(3) swipe(0.5, 0.9, 0.5, 0.1) sleep(0.5) swipe(0.5, 0.9, 0.5, 0.1) try: # swipe(0.5, 0.9, 0.5, 0.5) driver.implicitly_wait(10) driver.find_element_by_xpath('//*[@text=\'查看更多好友\']').click() driver.implicitly_wait(10) friendsLists = driver.find_elements_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget' '.LinearLayout/android.widget.FrameLayout/android.widget' '.RelativeLayout/android.widget.RelativeLayout[' '2]/android.widget.RelativeLayout/android.widget.RelativeLayout' '/android.widget.FrameLayout/com.uc.webview.export.WebView/com' '.uc.webkit.ax/android.webkit.WebView/android.view.View/android' '.view.View/android.view.View[2]/android.view.View') friendsNum = friendsLists except NoSuchElementException: log.logger.info('没找到好友') else: log.logger.info('好友共有%s人' % len(friendsNum)) return friendsNum
def openAntFroest(): log.logger.info('进入蚂蚁森林') try: driver.implicitly_wait(10) driver.find_element_by_xpath('//*[@text=\'蚂蚁森林\']').click() driver.implicitly_wait(10) except NoSuchElementException: log.logger.info('蚂蚁森林打开失败')
def stealEnergy(): log.logger.info('判断是否有能量') driver.implicitly_wait(10) items = driver.find_elements_by_class_name('android.widget.Button') if len(items) > 5: for i in items: if '能量' in i.text: log.logger.info('收取{1}'.format(i.text.replace('收集', ''))) i.click() back() else: back()
def clickFriendsPage(num): driver.implicitly_wait(10) friendsLists = driver.find_elements_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget' '.LinearLayout/android.widget.FrameLayout/android.widget' '.RelativeLayout/android.widget.RelativeLayout[' '2]/android.widget.RelativeLayout/android.widget.RelativeLayout' '/android.widget.FrameLayout/com.uc.webview.export.WebView/com' '.uc.webkit.ax/android.webkit.WebView/android.view.View/android' '.view.View/android.view.View[2]/android.view.View') driver.implicitly_wait(10) name = driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android' '.widget.FrameLayout/android.widget.RelativeLayout/android.widget' '.RelativeLayout[' '2]/android.widget.RelativeLayout/android.widget.RelativeLayout/android' '.widget.FrameLayout/com.uc.webview.export.WebView/com.uc.webkit.ax/android' '.webkit.WebView/android.view.View/android.view.View/android.view.View[' '1]/android.view.View/android.view.View[' '3]/android.view.View/android.view.View').text driver.implicitly_wait(10) add = '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android' \ '.widget.RelativeLayout/android.widget.RelativeLayout[' \ '2]/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/com.uc.webview' \ '.export.WebView/com.uc.webkit.ax/android.webkit.WebView/android.view.View/android.view.View/android.view' \ '.View[2]/android.view.View[' + str(num + 1) + ']/android.view.View[3]/android.view.View[' \ '1]/android.view.View ' friendName = driver.find_element_by_xpath(add).text log.logger.info('进入第%s个好友%s的森林' % (num + 1, friendName)) driver.implicitly_wait(10) if name == friendName: log.logger.info('第%s为自己的森林,跳过' % (num + 1)) return False else: friendsLists[num].click() return True
def login(username, password): username = username password = password log.logger.info(' ========================开始登录========================') # 判断是否登陆成功 try: # 隐示等待 driver.implicitly_wait(5) driver.find_element_by_xpath('//*[@text=\'蚂蚁森林\']') log.logger.info('登陆成功') except NoSuchElementException: # 跳过温馨提示 try: log.logger.info('跳过温馨提示') # driver.find_element_by_xpath('//*[@text=\'温馨提示\']') driver.find_element_by_xpath('//*[@text=\'同意\']').click() except NoSuchElementException: log.logger.info('未找到温馨提示') # 显示等待“其他登录方式”元素 WebDriverWait(driver, 5).until( lambda x: x.find_element_by_xpath('//*[@text=\'其他登录方式\']')) # 尝试使用“其他登录方式”登录 try: driver.find_element_by_xpath('//*[@text=\'其他登录方式\']').click() log.logger.info('使用账号密码登录') except NoSuchElementException: log.logging.info('未找到其他登录方式') # 控制输入账号及密码 try: log.logger.info('输入账号') usernameText = driver.find_element_by_id( 'com.ali.user.mobile.security.ui:id/userAccountInput') usernameText.find_element_by_id( 'com.ali.user.mobile.security.ui:id/content').send_keys( username) driver.find_element_by_id( 'com.ali.user.mobile.security.ui:id/nextButton').click() except NoSuchElementException: log.logger.info('未找到账号输入框') try: try: driver.implicitly_wait(5) log.logger.info('输入密码') passwordText = driver.find_element_by_id( 'com.ali.user.mobile.security.ui:id/userPasswordInput') passwordText.find_element_by_id( 'com.ali.user.mobile.security.ui:id/content').send_keys( password) driver.find_element_by_id( 'com.ali.user.mobile.security.ui:id/loginButton').click() except NoSuchElementException: log.logger.info('未找到密码输入框') except NoSuchElementException: log.logger.info('更换登录方式为密码') WebDriverWait(driver, 5).until( lambda x: x.find_element_by_xpath('//*[@text=\'换个方式登录\']')) driver.find_element_by_xpath('//*[@text=\'换个方式登录\']').click() driver.find_element_by_xpath('//*[@text=\'密码登录\']').click() driver.find_element_by_id( 'com.ali.user.mobile.security.ui:id/content').send_keys( password) driver.find_element_by_id( 'com.ali.user.mobile.security.ui:id/loginButton').click() # 取消升级 try: log.logger.info('取消升级') driver.find_element_by_xpath('//*[@text=\'稍后再说\']').click() log.logger.info( '========================登陆成功========================') except NoSuchElementException: log.logger.info( '========================登陆成功========================')
def back(): log.logger.info('返回') driver.implicitly_wait(10) driver.find_element_by_id('com.alipay.mobile.nebula:id/h5_tv_nav_back').click() sleep(2)
def stealEnergy(): log.logger.info('判断是否有能量') driver.implicitly_wait(10) items = driver.find_elements_by_class_name('android.widget.Button') if len(items) > 5: for i in items: if '能量' in i.text: log.logger.info('收取{1}'.format(i.text.replace('收集', ''))) i.click() back() else: back() if __name__ == '__main__': login('', '') openAntFroest() lists = openFriendsList() driver.implicitly_wait(10) for num in range(len(lists)): reName=clickFriendsPage(num) if not reName: pass else: stealEnergy() sleep(1) # back() swipe(0.5, 0.5, 0.5, 0.4) log.logger.info('收取完成')