appium webdriver 基本操作及小例子等
#encoding=utf-8 ''' ''' #driver新建 driver=webdriver.Remote('http://localhost:4723/wd/hub',caps) #关闭driver driver.quit() #find_element_by_id box=driver.find_element_by_id('com.xsteach.appedu:id/rl_home_top_search') box=driver.find_element_by_id('rl_home_top_search') #class_name driver.find_element_by_class_name('android.widget.TextView') #这个方式获取到的是一个列表 driver.find_elements_by_android_uiautomator('new UiSelector().className("android.widget.TextView")')[2] #xpath driver.find_element_by_xpath("//android.widget.RadioButton[@text='个人中心']") #text 同className方式,结果是一个list driver.find_elements_by_android_uiautomator('new UiSelector().text("个人中心")') #textContains 不是list driver.find_element_by_android_uiautomator('new UiSelector().textContains("中心")').click() #正则匹配 不是list driver.find_element_by_android_uiautomator('new UiSelector().textMatches(".*的课程.*")').click() #*******************webview定位类似frame的操作方式**************** #link_text driver.find_element_by_link_text('新闻') driver.find_element_by_name('') #android原生页面没有name属性 #find_element(by,value) from selenium.webdriver.common.by import By driver.find_element(By.ID,'kw') cateid_locator=(By.ID,"ag2") driver.find_element(*cateid_locator).click() #*******************by的相关操作***************** By.ID 相当于by_id By.CLASS_NAME 相当于by_class_name By.XPATH 相当于by_xpath By.NAME 相当于by_name By.TAG_NAME 相当于by_tag_name By.CSS_SELECTOR 相当于by_css_selector By.LINK_TEXT 相当于by_link_text #find_elements_by_class_name 返回列表 driver.find_elements_by_class_name("android.widget.TextView")[10].click() #先找到元素一 再继续查找元素二 driver.find_element_by_class_name(“xxx”).find_element_by_name(“yyy") #清空clear driver.find_element_by_id('editText').clear() #***************左右上下滑动屏幕(x*3/4,y/4,x/4,y/4)************ #表示起始点坐标、终点坐标,参照点左上角 #左滑 x=driver.get_window_size()['width'] y=driver.get_window_size()['height'] driver.swipe(x*3/4,y/4,x/4,y/4) #右滑 driver.swipe(x/4,y/4,x*3/4,y/4) #下滑(与上滑有可能反了) driver.swipe(x/4,y/4,x/4,y*3/4) #上滑 driver.swipe(x/4,y*3/4,x/4,y/4) #**********TouchAction 类似ActionChains********** #滑动页面(解锁密码可以用) from appium.webdriver.common.touch_action import TouchAction def test_scroll_down(driver): screen = driver.get_window_size() action = TouchAction(driver) action.press(x=screen['width']/2,y=screen['height']/2) action.move_to(x=0,y=-screen['height']/10) action.release() action.perform() #***********获取界面属性,控件属性********** #当前的activity driver.current_activity #屏幕高宽,字典 driver.get_window_size() #获取控件的文本 mobile_name.get_attribute('text') 或者mobile_name.text #获取控件类型 mobile_name.get_attribute('className') #控件是否显示 mobile_name.is_displayed()或者 mobile_name.get_attribute('displayed') #获得控件的位置 mobile_name.location #获得控件的大小 mobile_name.size #截屏 driver.get_screenshot_as_file(filepath) #断言 assert '建筑设计' in driver.page_source #*********键盘事件*********** #模拟点击返回键 driver.press_keycode(4) appium可以借助pc端selenium的物理按键代码来操作。 driver.press_keycode() 括号内填入pc端键盘的代号 #键盘事件操作 driver.keyevent(66) #电话键 KEYCODE_CALL 拨号键 5 KEYCODE_ENDCALL 挂机键 6 KEYCODE_HOME 按键Home 3 KEYCODE_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 删除键 112 KEYCODE_INSERT 插入键 124 KEYCODE_TAB Tab键 61 KEYCODE_NUM_LOCK 小键盘锁 143 KEYCODE_CAPS_LOCK 大写锁定键 115 KEYCODE_BREAK Break/Pause键 121 KEYCODE_SCROLL_LOCK 滚动锁定键 116 KEYCODE_ZOOM_IN 放大键 168 KEYCODE_ZOOM_OUT 缩小键 169 组合键 KEYCODE_ALT_LEFT Alt+Left KEYCODE_ALT_RIGHT Alt+Right KEYCODE_CTRL_LEFT Control+Left KEYCODE_CTRL_RIGHT Control+Right KEYCODE_SHIFT_LEFT Shift+Left KEYCODE_SHIFT_RIGHT Shift+Right 基本 KEYCODE_0 按键’0’ 7 KEYCODE_1 按键’1’ 8 KEYCODE_2 按键’2’ 9 KEYCODE_3 按键’3’ 10 KEYCODE_4 按键’4’ 11 KEYCODE_5 按键’5’ 12 KEYCODE_6 按键’6’ 13 KEYCODE_7 按键’7’ 14 KEYCODE_8 按键’8’ 15 KEYCODE_9 按键’9’ 16 KEYCODE_A 按键’A’ 29 KEYCODE_B 按键’B’ 30 KEYCODE_C 按键’C’ 31 KEYCODE_D 按键’D’ 32 KEYCODE_E 按键’E’ 33 KEYCODE_F 按键’F’ 34 KEYCODE_G 按键’G’ 35 KEYCODE_H 按键’H’ 36 KEYCODE_I 按键’I’ 37 KEYCODE_J 按键’J’ 38 KEYCODE_K 按键’K’ 39 KEYCODE_L 按键’L’ 40 KEYCODE_M 按键’M’ 41 KEYCODE_N 按键’N’ 42 KEYCODE_O 按键’O’ 43 KEYCODE_P 按键’P’ 44 KEYCODE_Q 按键’Q’ 45 KEYCODE_R 按键’R’ 46 KEYCODE_S 按键’S’ 47 KEYCODE_T 按键’T’ 48 KEYCODE_U 按键’U’ 49 KEYCODE_V 按键’V’ 50 KEYCODE_W 按键’W’ 51 KEYCODE_X 按键’X’ 52 KEYCODE_Y 按键’Y’ 53 KEYCODE_Z 按键’Z’ 54 ************************小例子************************* #某教育app的例子 from appium import webdriver import time caps = {} caps["appPackage"] = "com.xsteach.appedu" caps["appActivity"] = "com.xsteach.appedu.StartActivity" caps["platformName"] = "Android" caps["platformVersion"] = '6.0.1' caps["deviceName"] = '3HX0217115011233' caps["unicodeKeyboard"] = True caps["autoAcceptAlerts"] = True #对权限弹窗进行授权 caps["resetKeyboard"] = True caps["noReset"] = True caps["newCommandTimeout"]=6000 driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) #获取手机屏幕大小,返回字典 {'width': 720, 'height': 1280} size = driver.get_window_size() print(size) print(driver.is_app_installed("com.xsteach.appedu")) #关闭app driver.close_app() driver.quit() #***************************模拟某教育APP 搜索操作************************** from appium import webdriver import time caps = {} caps["appPackage"] = "com.xsteach.appedu" caps["appActivity"] = "com.xsteach.appedu.StartActivity" caps["platformName"] = "Android" caps["platformVersion"] = '8' caps["deviceName"] = '3HX0217115011233' caps["unicodeKeyboard"] = True caps["autoAcceptAlerts"] = True #对权限弹窗进行授权 caps["resetKeyboard"] = True caps["noReset"] = True caps["newCommandTimeout"]=6000 driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) #获取手机大小{'width': 1080, 'height': 1808} size = driver.get_window_size() print(size) # 查看指定包名的app是否已经按照 print(driver.is_app_installed("com.xsteach.appedu")) #driver.start_activity('com.xsteach.appedu', '.StartActivity') # 打印一下当前页面的activity print(driver.current_activity) time.sleep(5) uList = driver.find_elements_by_android_uiautomator("new UiSelector().text(\"个人中心\")") print(uList[0].text) box=driver.find_element_by_id("com.xsteach.appedu:id/rl_home_top_search") box.click() time.sleep(2) box=driver.find_element_by_id("com.xsteach.appedu:id/editText") time.sleep(2) box.send_keys("python") time.sleep(4) driver.find_element_by_id("com.xsteach.appedu:id/item_tv_dynamic_search_result").click() #随便选了一个下拉结果点击选择 time.sleep(3) #关闭app driver.close_app() #2、也可以跳过某些场景,达到某个页面 # element = driver.find_element_by_android_uiautomator('new UiSelector().text("新用户")') # element.click() print(driver.current_activity) ele = driver.start_activity('com.xsteach.appedu', '.StartActivity') print(driver.current_activity) time.sleep(5) box=driver.find_element_by_id("com.xsteach.appedu:id/rl_home_top_search") box.click() time.sleep(2) box=driver.find_element_by_id("editText") time.sleep(1) box.send_keys("设计") time.sleep(3) box.clear() box.send_keys("编程") driver.find_element_by_id("tv_Text").click() ******************************登录的完整例子******************************* from appium import webdriver import time import os caps = {} caps["appPackage"] = "com.xsteach.appedu" caps["appActivity"] = "com.xsteach.appedu.StartActivity" caps["platformName"] = "Android" caps["platformVersion"] = '8' caps["deviceName"] = '3HX0217115011233' caps["unicodeKeyboard"] = True caps["autoAcceptAlerts"] = True #对权限弹窗进行授权 caps["resetKeyboard"] = True caps["noReset"] = True caps["newCommandTimeout"]=6000 driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) #等待5秒 driver.implicitly_wait(5) person_center =driver.find_element_by_id("content_rb_mine") #person_center = driver.find_elements_by_android_uiautomator("new UiSelector().text(\"+个人中心\")") person_center.click() log_in = driver.find_element_by_id("tvLogin") log_in.click() driver.find_element_by_id("etUser").send_keys("fosterwu") driver.find_element_by_id("etPwd").send_keys("gloryroad123") driver.find_element_by_id("btnLogin").click() time.sleep(3) driver.close_app() *****************************chrome百度搜索的一个例子*************************** #注意chrome 与chromedriver的版本匹配问题 https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md #chromedriver版本替换 http://blog.sina.com.cn/s/blog_6a8557730102y17x.html from appium import webdriver from selenium.webdriver.support.ui import WebDriverWait import time,os url='https://www.baidu.com' desired_caps = { 'platformName': 'Android', 'deviceName': '3HX0217115011233', 'platformVersion': '8', 'noReset': True, 'browserName':'Chrome', "newCommandTimeout": "6000" } driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) print('浏览器启动成功') driver.get("http://www.baidu.com") #time.sleep(2) #华为手机,会弹出个权限的系统提示框,执行两次回车动作可以取消掉。 driver.keyevent(66) driver.keyevent(66) try: WebDriverWait(driver,10,1).until(lambda x:x.find_element_by_xpath("//input[@id='index-kw']")).send_keys("光荣之路") #xpath 定位如何获取到的 time.sleep(5) driver.keyevent(66) except Exception as err: pass driver.keyevent(66) time.sleep(5) assert "光荣之路" in driver.page_source driver.close_app() https://www.cnblogs.com/helenMemery/p/7346485.html desired_caps['recreateChromeDriverSessions'] = True启动时,这个参数也加上,他这是每到一个webview页面就重新创建一个driver,要不然定位找不到 1.手机端打开需要获取的webview页面 2. pc端chrome浏览器地址栏中输入:chrome://inspect/#devices,定位元素 但是需要FQ,如果能FQ,就可以加载出来 https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md : 这个是appium在GitHub上对webview和chromedriver版本的说明 报stableObject异常时候,加个sleep. 切换webview的操作,没有查到怎么切换回去,可以先记录一下 之前的contexts,再切回之前的 网上查到了这个: self.driver.switch_to.context() 如果是从App转到web的话()填"WEBVIEW_1", 如果web转到App的话填"NATIVE_APP" contexts = driver.contexts print contexts c = contexts[1] print c driver.switch_to.context(c) now = driver.current_context print (now)