環境: 安卓7,夜神模擬器,微信7.0.7
代碼:
import os
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 連接手機
PLATFROM = 'Android'
DEVICE_NAME = '127.0.0.1:62001 device'
APP_PACKGE = 'com.tencent.mm'
APP_ACTIVITY = '.ui.LauncherUI'
DRIVER_SERVER = 'http://localhost:4723/wd/hub'
TIMEOUT = 300
class Moments():
def __init__(self):
# 驅動配置
self.desired_caps = {
'platformName': PLATFROM,
'deviceName': DEVICE_NAME,
'appPackage': APP_PACKGE,
'appActivity': APP_ACTIVITY,
"noReset": 'true'
}
self.driver = webdriver.Remote(DRIVER_SERVER, self.desired_caps)
self.wait = WebDriverWait(self.driver, TIMEOUT)
def enter(self):
# 此處是一個坑,sleep時間如果設置的短,將會找不到元素,因爲此時微信並沒有啓動
time.sleep(10)
el1 = self.driver.find_element_by_xpath("//android.widget.FrameLayout[@content-desc=\"當前所在頁面,與的聊天\"]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.RelativeLayout[3]/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.ImageView")
el1.click()
time.sleep(3)
el2 = self.driver.find_element_by_xpath("//android.widget.FrameLayout[@content-desc=\"當前所在頁面,與的聊天\"]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.mm.ui.mogic.WxViewPager/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.ListView/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout")
el2.click()
self.crawl()
def crawl(self):
# 滑動點
FLICK_START_X = 300
FLICK_START_Y = 300
FLICK_DISTANCE = 600
while True:
# 上滑
self.driver.swipe(FLICK_START_X, FLICK_START_Y + FLICK_DISTANCE, FLICK_START_X, FLICK_START_Y)
# 當前頁面顯示的所有狀態
items = self.wait.until(
EC.presence_of_all_elements_located((By.XPATH, '//android.widget.FrameLayout[@content-desc="當前所在頁面,朋友圈"]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ListView')))
# 遍歷每條狀態
for item in items:
try:
# 暱稱
nickname = item.find_element_by_id('com.tencent.mm:id/bag').get_attribute('text')
# 正文
content = item.find_element_by_id('com.tencent.mm:id/f3o').get_attribute('text')
if content == '':
pass
else:
print(nickname+':', content)
f = open('contents.txt','a',encoding='utf-8')
f.write('%s' % nickname + ':' +'%s' % content)
f.write('\n' + '-------------------------------------------------------------------------------------------------------------------------------------------------' + '\n')
time.sleep(3)
except NoSuchElementException:
pass
if __name__ == "__main__":
start = Moments()
start.enter()
坑:
1、遇到找不到元素的情況可以考慮一下app的啓動問題(指定頁面沒有打開的話,是找不到元素的),可以加time.sleep()解決。
2、安卓5老是連接失敗(Could not proxy command to remote server. Original error:socket hang up)(安卓7,安卓9沒問題),但是可以獲取到微信朋友圈的時間信息,安卓7以上,無法獲取到時間信息。