本篇开始介绍元素操作,先来看看元素点击和元素输入行为。前面我们已经知道了元素点击的方法是click(),我们直接在前面代码基础上,用send_keys()方法给输入框输入文字。结果,我这边报错了。
1. 编写send_keys()脚本,运行报错。
- import os
- import time
- from appium import webdriver
- apk_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) # 获取当前项目的根路径
- desired_caps ={ 'platformName': 'Android',
- 'platformVersion': '6.0.1',
- 'deviceName': 'KIW-AL10',
- 'noReset': True,
- 'appPackage': 'com.baidu.searchbox',
- 'appActivity': 'com.baidu.searchbox.SplashActivity',
- 'unicodeKeyboard': True,
- 'resetKeyboard': True
- }
- driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)#启动app
- time.sleep(3) #app启动后等待3秒,方便元素加载完成
- # 根据元素xpath来定位
- # 点击“输入框”
- driver.find_element_by_id("com.baidu.searchbox:id/baidu_searchbox").click()
- # 输入字段
- searchInputBox = driver.find_element_by_id('com.baidu.searchbox:id/SearchTextInput')
- searchInputBox.send_keys("Appium")
降级和重新安装
打开cmd分别输入以下命令:
1. python -m pip uninstall selenium
2. python -m pip install selenium==3.3.1
我原来selenium最新版本是3.4.3,重新安装3.3.1之后,运行脚本,测试通过,发现能够在百度输入框输入“Appium”字段。从这个问题来看,我们是不是忽略了一个问题,appium运行环境有一个前提条件是不是需要安装selenium?这个问题,我来测试下。继续cmd采用上面命令来卸载selenium3.3.1版本,运行一下,果然报错:ModuleNotFoundError: No module named 'selenium',查看appium源码,确实使用selenium,用了from selenium import webdriver。所以,这里记住,Appium安装环境,还需要安装selenium。如果做过selenium的同学,可能不会里面遇到这个错误提示,但是新手学appium和没有用过selenium的人,运行元素定位的脚本就会报这个错误。看来,先学Selenium,然后学Appium是很有优势的。