1. 確保手機已與電腦連接
2. 開啟微信debug模式
在微信聊天界面輸入:debugx5.qq.com,勾選"打開TBS內核Inspector調試功能”
3. 查看微信里面webview版本
在電腦chrome瀏覽器輸入:chrome://inspect/#devices,再打開微信的公眾號頁面,刷新瀏覽器頁面,就會出現webview版本號,點擊“inspect”即可定位元素
4. 下載對應版本的chromedriver
下載的對應版本的chromedriver替換該路徑:\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win下的chromedriver
5. 獲取公眾號的進程名字
adb shell
dumpsys activity top | grep ACTIVITY
ps 進程的pid
6. 實現代碼如下:
from appium import webdriver import time di = {} di['platformName'] = 'Android' di['platformVersion'] = '6.0' di['deviceName'] = 'Appium' di['unicodeKeyboard'] = True di['resetKeyboard'] = True di['appPackage'] = 'com.tencent.mm' di['appActivity'] = '.ui.LauncherUI' di['udid'] = '127.0.0.1:62001' # di['automationName']='uiautomator2' #不同點 di[ 'chromeOptions']="{'androidProcess': 'com.tencent.mm:tools'}" driver = webdriver.Remote('http://localhost:4723/wd/hub',di) time.sleep(5) driver.implicitly_wait(20) # 點微信首頁搜索按鈕 driver.find_element_by_accessibility_id("搜索").click() # 輸入內容搜索 time.sleep(3) driver.find_element_by_id('com.tencent.mm:id/hx').send_keys("yx") # 點開公眾號 time.sleep(3) driver.find_element_by_id('com.tencent.mm:id/l7').click() # 點公眾號菜單-精品分類 time.sleep(3) driver.find_elements_by_id('com.tencent.mm:id/aaq')[0].click() #不同點,切換到webview time.sleep(2) print(driver.contexts) driver.switch_to.context('WEBVIEW_com.tencent.mm:tools') time.sleep(3) #切換句柄 handles=driver.window_handles driver.switch_to_window(handles[1]) driver.find_element_by_xpath(".//*[@id='namespace_1']/div[1]/div/div[2]").click()