7.Appium最常用的方法总结

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 查看

原创文章 100 获赞 339 访问量 56万+

猜你喜欢

转载自blog.csdn.net/MATLAB_matlab/article/details/105816616