1.获取driver属性
- current_package:获取包名
- current_activity:获取当前活动页面 ==>url
- context:上下文 web窗口切换,H5测试获取上下文 driver.switch_to.context
- contexts:所有的上下文 ==>window_handers
- current_context:获取现在的上下文 获取当前窗口current_window_hander
- page_source:获取源码 XML
- capabilities:获取设置的capabilities
- device_time:设备时间
- location:定位
import time
from appium.webdriver import Remote
# 启动一次请求
caps = {"platformName": "Android",
"udid": "emulator-5554",
"appPackage": "com.lemon.lemonban",
"appActivity": ".activity.WelcomeActivity"
}
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities=caps)
# 设置隐性等待
driver.implicitly_wait(10)
# 获取app当前活动页面名称
print(driver.current_activity)
# 获取源代码XML
driver.page_source
# 获取定位
print(driver.location)
# 获取设置的capabilities
print(driver.capabilities)
driver.quit()
运行结果:
2.获取元素属性
- text:获取元素的文本
- rect:获取元素的尺寸
import time
from appium.webdriver import Remote
# 启动一次请求
# 1.指明需要连接的服务地址和端口号
# 2.You must include a platformName capability
caps = {"platformName": "Android",
"udid": "emulator-5554",
"appPackage": "com.lemon.lemonban",
"appActivity": ".activity.WelcomeActivity"
}
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities=caps)
# 设置隐性等待
driver.implicitly_wait(10)
el = driver.find_element('xpath', '(//*[@resource-id="com.lemon.lemonban:id/category_description"])[8]')
# 获取元素的文本
print(el.text)
# 获取元素的尺寸
print(el.rect)
time.sleep(3)
driver.quit()
运行结果:
3.用户操作
- click() 点击
- send_keys() 输入
用法跟selenium中的用法是一样的,但有个注意点:如果想输入中文,必须要设置两个参数
4.触屏操作
- tap:相当于全局的click,适合封装在basepage中
- move_to
- press
- long_press
- release
- wait
- flick</