第二章002 定位元素

一、常用的定位元素方式

1、通过id

2、通过name

3、通过classname,常用于定位一组元素

4、通过xpath

二、获取元素属性

.text

.getattribute("text")

 1 #coding=utf-8
 2 from appium import webdriver
 3 import time
 4 
 5 desired_caps = {}
 6 desired_caps['platformName'] = 'Android'
 7 desired_caps['platformVersion'] = '5.1'
 8 desired_caps['deviceName'] = '6T3HMU162P007147'
 9 desired_caps['appPackage'] = 'com.Qunar'#被测app包名
10 desired_caps['appActivity'] = 'com.mqunar.splash.SplashActivity'#被测app的activity
11 desired_caps['unicodeKeyboard'] = True  #绕过系统自带的键盘
12 desired_caps['resetKeyboard'] = True
13 desired_caps['newCommandTimeout'] = 7200
14 
15 driver = webdriver.Remote('http://127.0.0.1:4725/wd/hub', desired_caps)#启动app
16 time.sleep(15)#增加等待时间,否则可能报错,找不到元素
17 
18 #进入汽车票
19 driver.find_element_by_id('com.mqunar.atom.alexhome:id/atom_alexhome_mod_bus_ticket').click()
20 time.sleep(5)
21 
22 #获取默认出发地
23 dep = driver.find_element_by_id("com.mqunar.atom.bus:id/atom_bus_tv_dep_city")
24 print dep.get_attribute("text")
25 
26 #获取默认到达地
27 arr = driver.find_element_by_id("com.mqunar.atom.bus:id/atom_bus_tv_arr_city")
28 print arr.get_attribute("text")
29 #获取默认日期
30 print driver.find_element_by_id("com.mqunar.atom.bus:id/atom_bus_tv_dep_date").text
31 
32 #通过xpath定位搜索,并点击搜索
33 driver.find_element_by_xpath("//android.widget.Button[@text='搜  索']").click()
34 time.sleep(5)
35 
36 #xpath定位汽车票,点击进入车次详情
37 driver.find_element_by_xpath("//android.widget.LinearLayout[@index='3']").click()
38 time.sleep(10)
39 
40 #name定位预订车次,点击进入预约界面
41 driver.find_element_by_name(u"汽车票预订").click()
42 time.sleep(5)

三、元素赋值

1、屏蔽软键盘

增加两行代码

代码如下

desired_caps['unicodeKeyboard'] = True  #屏蔽软键盘
desired_caps['resetKeyboard'] = True

2、输入中文

在前面加上小u,如driver.find_element_by_id("com.mqunar.patch:id/pub_pat_title_etSearch").send_keys(u"上海")

 

posted @ 2018-01-11 18:36  蓝裙子  阅读(144)  评论(0编辑  收藏  举报