Appium腳本的運行
1.Pycharm新建一個test_appium包,并在包中新建test_xueqiu.py文件。
2.將上一步我們錄制好的腳本代碼,粘貼到test_xueqiu.py文件中。
3.安裝Appium-Python-Clinte模塊。
4.打開Nox模擬器。
5.打開Appium Desktop,點擊Start Server。
6.回到Pycharm,test_xueqiu.py右鍵,點擊run。稍等片刻程序自動運行
我們會看到一個報錯
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

尋找原因,是某個元素沒有找到。

這個元素在我們錄制時,是一個活動彈窗?,F(xiàn)在活動過期彈窗消失,我們注釋這段代碼即可。
有時則是因為打開頁面的過程中,加載速度比較慢,導(dǎo)致找不到元素。我們可以加一個隱形等待xx秒(代碼加在
driver=webdriver.Remote()下方)
# 隱形等待10s
driver.implicitly_wait(10)
自動生成的一般都是比較簡陋的腳本,那么接下來,我們對腳本進行一定的改造
Appium腳本的簡單改造之第一步
# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
import time
from appium import webdriver
class TestXueqiu:
# 初始化需要的準備工作
def setup(self):
caps = {}
caps["platformName"] = "Android"
caps["deviceName"] = "Nox"
caps["appPackage"] = "com.xueqiu.android"
caps["appActivity"] = ".view.WelcomeActivityAlias"
caps["ensureWebviewsHavePages"] = True
# 將driver變?yōu)閷嵗兞?。后面需要用? self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
self.driver.implicitly_wait(10)
def test_profile(self):
el1 = self.driver.find_element_by_id("com.xueqiu.android:id/tv_agree")
el1.click()
# el2 = driver.find_element_by_id("com.xueqiu.android:id/ib_close")
# el2.click()
el3 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TabHost/android.widget.LinearLayout/android.widget.TabWidget/android.widget.RelativeLayout[2]/android.widget.ImageView")
el3.click()
# 收尾工作
def teardown(self):
# 為了觀察,睡眠10秒后再退出
time.sleep(10)
self.driver.quit()
有時我們在運行時,會出現(xiàn)權(quán)限的彈框,如獲取IMEI號、獲取電話和短信權(quán)限等??梢栽谀_本中點擊這個元素同意,當然也有一種比較省力的方法,使用autoGrantPermissions屬性自動授予權(quán)限

caps["autoGrantPermissions"] = True
如果你加入了這個屬性,那么之前一些點擊權(quán)限的元素操作可以注釋或刪除掉
Appium腳本的簡單改造之第二步
我們看到find_element_by_xpath非常的長,看起來給人一種非常不舒服的感覺。同時也不利于之后的維護工作。那么我們怎么獲取到這個元素的類型呢?
el3 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TabHost/android.widget.LinearLayout/android.widget.TabWidget/android.widget.RelativeLayout[2]/android.widget.ImageView")
有兩種方法:
1.使用Appium Desktop幫助我們分析
我們點擊行情,看到右側(cè)的resource-id為
com.xueqiu.android:id/tab_icon。那么這個tab_icon可以是我們可以注意的對象。也就是說可以通過id定位找到這個元素

接下來,我們點擊Appium Desktop上方放大鏡功能,輸入id tab_icon,搜索后可以看到有5個元素,分別對應(yīng)了雪球下方導(dǎo)航欄的五個元素。其中第二個是


那么我們是否可以這樣寫呢?
el3 = self.driver.find_element_by_id("tab_icon")[1]
el3.click()
如果這樣寫,那么就會掉入一個坑里。因為這樣寫會報錯:
TypeError: 'WebElement' object is not subscriptable
翻譯過來就是說,WebElement對象不可下標。
find_element將始終返回單個WebElement。因此,無法通過任何索引訪問元素,例如[0]、[1]等作為 index。只有l(wèi)ist才可以做索引
所以,我們不能使用find_element_by_id,而需要使用find_elements_by_id,這樣返回的才是list而不是單個WebElement。
el3 = self.driver.find_elements_by_id("tab_icon")[1]
el3.click()
2.使用AndroidSDK\tools\bin\uiautomatorviewer.bat工具,它類似于Chrome的檢查功能。
首先啟動Nox,打開雪球APP。雙擊運行uiautomatorviewer.bat,點擊左上角第二個圖標進行屏幕獲取。
同樣的,我們定位到需要的元素,右側(cè)查看元素屬性。
我們可以發(fā)現(xiàn)uiautomatorviewer比Appium運行速度快。需要哪個工具可以根據(jù)自己情況選擇。
這里要注意,使用xpath層級定位時,uiautomatorviewer會出現(xiàn)缺少層級的情況,建議使用Appium Desktop檢查層級關(guān)系

- Tips:
1.如果不設(shè)置參數(shù),即默認,那么測試后將停止并清除應(yīng)用數(shù)據(jù),不卸載apk
2.fullReset = True時,在會話開始前 測試后 停止 app,清除 app 數(shù)據(jù)并卸載 apk
3.noReset = True時,不停止應(yīng)用程序,不清除應(yīng)用數(shù)據(jù),不卸載 apk