Appium常用到的方法
1.click 點擊
element=driver.find_element_by_xpath('xxx')
element.click()
或者
driver.find_element_by_xpath('xxx').click()
2.輸入鍵值 press_keycode
driver.press_keycode(4) #點擊返回鍵
除了press_keycode方法,還有其他方法可以輸入鍵值,如keyevent()和long_press_keycode()
keycode:鍵值,http://developer.android.com/reference/android/view/KeyEvent.html
鍵名 |
描述 |
鍵值 |
KEYCODE_CAL |
撥號鍵 |
5 |
KEYCODE_ENDCAL |
掛機鍵 |
6 |
KEYCODE_HOME |
HOME鍵 |
3 |
KEYVODE_MENU |
菜單鍵 |
82 |
KEYCODE_BACK |
返回鍵 |
4 |
KEYCODE_SEARCH |
搜索鍵 |
84 |
KEYCODE_CAMERA |
拍照鍵 |
27 |
KEYCODE_FOCUS |
拍照對焦鍵 |
80 |
KEYCODE_POWER |
電源鍵 |
26 |
KEYCODE_NOTIFICATION |
通知鍵 |
83 |
KEYCODE_MUTE |
話筒靜音鍵 |
91 |
KEYCODE_VOLUME_MUTE |
揚聲器靜音鍵 |
164 |
KEYCODE_VOLUME_UP |
音量增加鍵 |
24 |
KEYCODE_VOLUME_DOWN |
音量減小鍵 |
25 |
KEYCODE_ENTER |
回車鍵 |
66 |
KEYCODE_ESCAPE |
ESC鍵 |
111 |
KEYCODE_DPAD_CENTER |
導航鍵 確定鍵 |
23 |
KEYCODE_DPAD_UP |
導航鍵 向上 |
19 |
KEYCODE_DPAD_DOWN |
導航鍵 向下 |
20 |
KEYCODE_DPAD_LEFT |
導航鍵 向左 |
21 |
KEYCODE_DPAD_RIGHT |
導航鍵 向右 |
22 |
KEYCODE_MOVE_HOME |
光標移動到開始鍵 |
122 |
KEYCODE_MOVE_END |
光標移動到末尾鍵 |
123 |
KEYCODE_PAGE_UP |
向上翻頁鍵 |
92 |
KEYCODE_PAGE_DOWN |
向下翻頁鍵 |
93 |
KEYCODE_DEL |
退格鍵 |
67 |
KEYCODE_FORWARD_DEL |
刪除鍵 |
122 |
KEYCODE_INSERT |
插入鍵 |
124 |
KEYCODE_TAB |
Tab鍵 |
61 |
KEYCODE_NUM_LOOK |
小鍵盤鎖 |
143 |
KEYCODE_CAPS_LOCK |
大寫鎖定鍵 |
115 |
KEYCODE_BREAK |
Break/Pause鍵 |
121 |
KEYCODE_SCROLL_LOCK |
滾動鎖定鍵 |
116 |
KEYCODE_ZOOM_IN |
放大鍵 |
168 |
KEYCODE_ZOOM_OUT |
縮小鍵 |
169 |
3.獲取當前窗口大小get_window_size
driver.get_window_size()[‘xx’]
width=driver.get_window_size()['width'] #獲取屏幕的寬
height=driver.get_window_size()['height'] #獲取屏幕的高
4.滑動 swip
driver.swip(start_x,start_y,end_x,end_y,duration=None)
按住A點(start_x,start_y)快速滑動至B點(end_x,end_y),參數是座標值 duration表示移動的時間間隔
PS:以左上角爲原點
5.啓動另一個activity start_activity()
這個可以是啓動同應用的另一個activity,也可以啓動另一個應用的activity
dirver.strat_activity(‘包名’,’activity名’)
from appium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException
def android_driver():
desired_cap={}
desired_cap['platformName']="Android"
desired_cap['platformVersion']='8.1.0'
desired_cap['deviceName']='HONOR9X'
desired_cap['udid']='5fb5c4cc' #ip或者設備好
desired_cap['appPackage']='com.ss.android.article.news' #要啓動的應用包名
desired_cap['appActivity']='com.ss.android.article.news.searchIcon2' #要啓動的應用activity
desired_cap['noReset']=True #不需要重新登錄
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_cap)
return driver
if __name__=="__main__":
driver=android_driver()
time.sleep(10)
# 跨應用,進入愛奇藝首頁
driver.start_activity('com.qiyi.video', 'com.qiyi.video.WelcomeActivity')
time.sleep(10)
# 同應用進入搜索
driver.start_activity('com.ss.android.article.news', 'com.ss.android.article.base.feature.search.SearchActivity')
time.sleep(10)
6.截圖 get_screenshot_as_file
driver.get_screenshot_as_file(‘路徑.png')
driver.get_screenshot_as_file("1.png") #保存到當前目錄
7.獲取元素屬性值
element.text #獲取元素的text屬性值
element.content-desc #獲取元素content-desc屬性值
from appium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException
def android_driver():
desired_cap={}
desired_cap['platformName']="Android"
desired_cap['platformVersion']='8.1.0'
desired_cap['deviceName']='HONOR9X'
desired_cap['udid']='5fb5c4cc' #ip或者設備好
desired_cap['appPackage']='com.ss.android.article.news' #要啓動的應用包名
desired_cap['appActivity']='com.ss.android.article.news.searchIcon2' #要啓動的應用activity
desired_cap['noReset']=True #不需要重新登錄
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_cap)
return driver
if __name__=="__main__":
driver=android_driver()
time.sleep(10)
try:
element=driver.find_element_by_xpath('//*[@resource-id="com.ss.android.article.news:id/b8b"]')
except NoSuchElementException:
print("沒有找到這個元素")
else:
print("已經找到這個元素了,提取他的text屬性值")
text_name=element.text
print(text_name) #輸出首頁
8.查找元素使用智能等待 WebDriverWait
element=WebDriverWait(driver,wait_num).until(lambda x:x.find_element_by_xpath(xxx))
在查找元素時,如果頁面還沒有加載出來就去查找就會報沒有找到元素的異常(NoSuchElementException),這時我們可以加入time.sleep()進行等待,等待過後再去查找元素,這樣的就會導致自動化時間過長,每一步都要加一個等待。那麼有沒有一種比較智能的查找元素的方法呢,他能夠智能的等待,如果找到元素了就直接返回,如果沒有找到元素纔等到,WebDriverWait可以實現這種功能。
wait_num參數就是等待的最長時長,如果超過這個時長還沒有找到元素,就會拋出一個超時的異常,如果找到了元素就直接返回,就不等待了,這樣既能找到元素,又能節省時間。
PS:在使用WebDriverWait之前,需要先導入
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from appium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
def android_driver():
desired_cap={}
desired_cap['platformName']="Android"
desired_cap['platformVersion']='8.1.0'
desired_cap['deviceName']='HONOR9X'
desired_cap['udid']='5fb5c4cc' #ip或者設備好
desired_cap['appPackage']='com.ss.android.article.news' #要啓動的應用包名
desired_cap['appActivity']='com.ss.android.article.news.searchIcon2' #要啓動的應用activity
desired_cap['noReset']=True #不需要重新登錄
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_cap)
return driver
if __name__=="__main__":
driver=android_driver()
try:
#element=driver.find_element_by_xpath('//*[@resource-id="com.ss.android.article.news:id/b8b"]')
element=WebDriverWait(driver,10).until(lambda x:x.find_element_by_xpath('//*[@resource-id="com.ss.android.article.news:id/b8b"]'))
except TimeoutException:
print("超時了,沒有找到這個元素")
else:
print("已經找到這個元素了,提取他的text屬性值")
text_name=element.text
print(text_name) #輸出首頁
通過對比可以明顯的發現時間縮短了,找到元素直接就返回。
詳情請點擊 https://study.163.com/course/introduction/1209833813.htm 查看