appium python 滑动+python日志怎么打

发现一个网址的内容比较好,就转过来了
#默认系统语言对应的Strings.xml文件内的数据。
get_app_string()
#查找某一个语言环境对应的字符串文件Strings.xml内数据
get_app_string(String language)
#按下某个键,具体哪个键由key值决定,key值定义在AndroidKeyCode类中
send_key_event(int key)
#获取当前activity,比如(.ApiDemos)
current_activity()
#根据bundleId来判断该应用是否已经安装
is_app_installed(String bundleId)
#安装app,appPath为应用的本地路径
install_app(String appPath)
#卸载app.bundleId在中代表的是报名,而在ios中有专门的bundleId号。
remove_app(String bundleId)
#关闭应用,其实就是按home键把应用置于后台
close_app()
launch_app()
#先closeApp然后在launchAPP
reset_app()
#将字符数组用64位格式写到远程目录的某个文件中。也可以理解为把本地文件push到设备上。
push_file(String remotePath, byte[] base64Data)
#将设备上的文件pull到本地硬盘上
pull_file(String remotePath)
#将设备上的文件夹pull到本地硬盘上,一般远程文件为/data/local/tmp下的文件。
pull_folder(String remotePath)
#设置的网络连接状态,可以开关蓝牙、wifi、数据流量。通过NetworkConnectionSetting中的属性来设置各个网络连接的状态。
set_network_connect(NetworkConnectionSetting connection)
#得到当前网络的状态
get_network_connection()
#ios隐藏键盘
hide_keyboard()
#隐藏键盘,只能用于ios上。
hide_keyboard(String strategy, String keyName)
#执行一个touch动作,该touch动作是由TouchAction封装的。
perform_touchAction(TouchAction touchAction)
#点击element控件中心点按下,duration*5毫秒秒后松开,如此重复fingers次。
tap(int fingers, WebElement element, int duration)
#点击(x,y)点按下,duration*5毫秒后松开,如此重复fingers次。
tap(int fingers, int x, int y, int duration)
#从(startx,starty)滑到(endx,endy),分duration步滑,每一步用时是5毫秒。
swipe(int startx, int starty, int endx, int endy, int duration)
#2个手指操作控件,从对角线向中心点滑动。
pinch(WebElement el)
#以(x,y)为基准,计算得出(x,y-100),(x,y+100)两个点,然后2个手指按住这两个点同时滑到(x,y)
pinch(int x, int y)
#与pinch(el)的动作刚好相反。两个手指由控件的中心点慢慢向控件的左顶点后右底点滑动。
zoom(WebElement el)
#和pinch(x,y)相反。两个手指从(x,y)点开始向(x,y-100)和(x,y+100)滑动。
zoom(int x, int y)
#锁屏多少秒后解锁
lock_screen(int seconds)
#模拟摇晃手机
#滚动到某个text属性为指定的字符串的控件
scroll_to(String text)
#滚动到某个text属性包含传入的字符串的控件
scroll_to_exact(String text)
#设置上下文
context(String name)
#可用上下文
get_context_handles()
#当前上下文
get_context()
#设置屏幕横屏或者竖屏
rotate(ScreenOrientation orientation)
#获取当前屏幕的方向
get_orientation()
#利用ios中的uiautomation中的属性来获取控件
find_element_by_ios_uiautomation(String using)
#和上面一样,不过获得的是多个控件
find_elements_by_ios_uiautomation(String using)
#利用android的uiautoamtor中的属性来获取单个控件。
find_element_by_android_uiautomator(String using)
#和上面一样,但是该方法获得是多个控件
find_elements_by_android_uiautomator(String using)
#利用accessibility id来获取单个控件
find_element_by_accessibilityid(String using)
#利用accessibility id来获得多个控件
find_elements_by_accessibilityid(String using)
'''AppiumDriver的辅助类,主要针对手势操作,比如滑动、长按、拖动等。
TouchAction的原理是讲一系列的动作放在一个链条中,然后将该链条传递给服务器。
服务器接受到该链条后,解析各个动作,逐个执行。'''
TouchAction()
#在控件上执行press操作
press(WebElement el)
#在坐标为(x,y)的点执行press操作
press(int x, int y)
#在控件el的左上角的x坐标偏移x单位,y左边偏移y单位的坐标上执行press操作。
press(WebElement el, int x, int y)
#释放操作,代表该系列动作的一个结束标志。
#以el为目标,从另一个点到该目标上
move_to(WebElement el)
#以(x,y)点为目标,从另一个点移动到该目标上
move_to(WebElement el, int x, int y)
#在控件的中心点上敲击一下
tap(WebElement el)
#在(x,y)点轻击一下
tap(int x, int y)
#以控件el的左上角为基准,x轴向右移动x单位,y轴向下移动y单位。在该点上轻击。
tap(WebElement el, int x, int y)
#代表一个空操作,等待一段时间
wait_action()
wait_action(int ms)
long_press(WebElement el)
long_press(int x, int y)
#偏移点长按
long_press(WebElement el, int x, int y)
#取消执行该动作
#执行该动作
http://www.51testing.com/html/15/n-1408815.html
阅读(...) 评论()7被浏览500分享邀请回答0添加评论分享收藏感谢收起0添加评论分享收藏感谢收起写回答本帖已被设为精华帖!
前面写了个《新手贴:Windows 平台上的使用 Java 语言实现 appium 自动化程序 for Android(完整版)》的帖子: ,针对python语言 也来看看如何实现。还是按照流水账的形式来描述。
一,环境配置篇
在Windows上配置
1)下载安装node.js() 安装的时候有选项,记得把环境变量添加到path路径
2)使用npm安装appium,运行CMD输入 npm install -g appium(有些朋友反应在cmd下运行npm无效,如果这样请把nodejs的目录添加到用户变量的path下重启cmd即可 参考帖子:) ,如下图:
3)下载安装Android SDK(环境变量指向 ANDROID_HOME%\tools 和% ANDROID_HOME%\platform-tools
4)安装JDK并设置JAVA_HOME环境变量
5)安装ANT,并将%ANT_HOME%\bin路径加到PATH
6)安装MAVEN,设置%M2_HOME%\bin,并添加到PATH中
7)安装Git
8)运行CMD 输入appium-doctor检查你的环境是不是都配置好了 如图:
整体的环境变量已经配置完毕,不过接下来要配置 python+selenium安装。
二,python+selenium安装配置:
1)下载并安装python去这个地址,发表文章时,我使用的是
2)下载并安装setuptools【这个工具是python的基础包工具】
去这个地址下载
3)去这个地址下载
4)使用CMD命令进入以上解压后的文件夹c:\pip,然后使用python setup.py install
a、如果python命令使用不成功,请配置下环境变量 就能OK(这个去百度一下吧。。。。)
b、报错no module named setuptools 可以下载一个运行ez_setup.py,运行ez_setup.py:python ez_setup.py ;
如果运行正常,那就安装成功了。)
参考图(运行结果不保证与该图完全一致):
5)再打开CMD命令,进入python的script路径,如本人的C:\Python\Scripts然后输入
命令:easy_install pip (恭喜你这边安装成功后,就可以顺利使用pip命令了)
参考图(运行结果不保证与该图完全一致):
6)直接使用pip安装selenium,命令为:pip install selenium -i (使用国内地址)
参考图(运行结果不保证与该图完全一致):
7)打开python的shell或者IDEL界面 ,输入from selenium import webdriver 如果不报错那就说明你已经安装selenium for python成功了。
安装过程也可以参考:
三,appium启动篇
由于我测试是连接真机的,所以这里需要先通过adb devices -l 命令获得 真机的udid号,详细步骤如下:
1)真机(安卓版本4.2.2)通过USB连接 电脑,驱动装好,打开USB调试模式
2)再在cmd中输入 appium -a 127.0.0.1 -p 4723 (-a表示ip,-p表示端口, 可以通过appium -h查看更多命令)
3)如果如下图所示 就表示 appium服务启动成功了,注意这个窗口不要关闭 因为这是appium的服务 关了就关了服务,后面过程无法执行,而且这个窗口也是 日志输出的窗口用于排错。
四,代码执行篇
这块我主要是执行的是官方的演示代码:通讯录管理app,安装打开app,并添加一个联系人保存的操作
1)首先去下载ContactManager.apk()
2)将官网的示例代码 android_contact.py 下载下来 放在
Python的目录
3)对python代码进行部分修改
from selenium import webdriver
# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
desired_caps = {}
desired_caps['device'] = 'Android'
desired_caps['browserName'] = ''
desired_caps['version'] = '4.2.2'
desired_caps['app'] = PATH('C:\Users\Stephen\Desktop\ContactManager.apk')
desired_caps['app-package'] = 'com.example.android.contactmanager'
desired_caps['app-activity'] = '.ContactManager'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
el = driver.find_element_by_name("Add Contact")
el.click()
textfields = driver.find_elements_by_tag_name("textfield")
textfields[0].send_keys("My Name")
textfields[2].send_keys("")
driver.find_element_by_name("Save").click()
driver.quit()
4)运行CMD,进入python目录,输入命令python android_contact.py 此时会自动安装apk并完成相应的添加联系人的操作
OK整个配置执行就算完成了
好帖,好贡献
顶一下好帖···
来一个nodejs版本的楼主··
最后一步运行CMD,进入python目录,输入命令python android_contact.py 此时会自动安装apk并完成相应的添加联系人的操作
我提示报错,请大侠帮忙看看是为何?
C:\Python27\Scripts&python C:\Python27\Scripts\android_contact.py Traceback (most recent call last): File "C:\Python27\Scripts\android_contact.py", line 17, in
driver = webdriver.Remote('', desired_caps) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 72, in init self.start_session(desired_capabilities, browser_profile) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 115, in start_session
'desiredCapabilities': desired_capabilities, File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 164, in execute response = self.command_executor.execute(driver_command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 347, in execute
return self._request(command_info[0], url, body=data) File "C:\Python27\lib\site packages\selenium\webdriver\remote\remote_connection.py", line 429, in _request
body = data.decode('utf-8').replace('\x00', '').strip() File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' codec can't decode byte 0xb4 in position 193: invalid start byte
python android_contact.py的代码如下:
from selenium import webdriver
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(file), p)
desired_caps = {}
desired_caps['device'] = 'android'#android selendroid
desired_caps['browserName'] = ''
desired_caps['version'] = '4.1.2'
desired_caps['app'] = PATH('D:\test\ContactManager.apk')
desired_caps['app-package'] = 'com.example.android.contactmanager'
desired_caps['app-activity'] = '.ContactManager'
driver = webdriver.Remote('', desired_caps)
el = driver.find_element_by_name("Add Contact")
el.click()
textfields = driver.find_elements_by_tag_name("textfield")
textfields[0].send_keys("My Name")
textfields[2].send_keys("")
driver.find_element_by_name("Save").click()
driver.quit()
回答3楼的问题 :
desired_caps['version'] = '4.1.2'
真机系统版本要&=4.2...
可行~~已跑上。
全部按要求配置完成,运行android_contact.py报如下错误,请大侠帮忙看看?谢谢!
C:\Users\cylboy&python D:\test\android_contact.py
Traceback (most recent call last):
File "D:\test\android_contact.py", line 17, in
driver = webdriver.Remote('', desired_caps)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 72, in init
self.start_session(desired_capabilities, browser_profile)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 115, in start_session
'desiredCapabilities': desired_capabilities,
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 164, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connectio
n.py", line 347, in execute
return self._request(command_info[0], url, body=data)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connectio
n.py", line 429, in _request
body = data.decode('utf-8').replace('\x00', '').strip()
File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb4 in position 193: invalid
start byte
你不贴个代码 真很难帮你。
Traceback (most recent call last):
File "C:\android_contact.py", line 17, in
driver = webdriver.Remote('', desired_caps)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 71, in init
self.start_session(desired_capabilities, browser_profile)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 113, in start_session
'desiredCapabilities': desired_capabilities,
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 164, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u"A new session could not be created. (Original error: Bad app: C:\C:\test\ContactManager.apk. App paths need to be absolute, or relative to the appium server install dir, or a URL to compressed file, or a special app name. cause: Error: Error locating the app: ENOENT, stat 'C:\C:\test\ContactManager.apk')"
from selenium import webdriver
Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(file), p)
desired_caps = {}
desired_caps['device'] = 'Android'
desired_caps['browserName'] = ''
desired_caps['version'] = '4.2.2'
desired_caps['app'] = PATH('C:\test\ContactManager.apk')
desired_caps['app-package'] = 'com.example.android.contactmanager'
desired_caps['app-activity'] = '.ContactManager'
driver = webdriver.Remote('', desired_caps)
el = driver.find_element_by_name("Add Contact")
el.click()
textfields = driver.find_elements_by_tag_name("textfield")
textfields[0].send_keys("My Name")
textfields[2].send_keys("")
driver.find_element_by_name("Save").click()
driver.quit()
不知道怎么弄了~
根据你的教程做的~
from selenium import webdriver
# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
desired_caps = {}
desired_caps['device'] = 'Android'
desired_caps['browserName'] = ''
desired_caps['version'] = '4.2.2'
desired_caps['app'] = PATH('C:\test\ContactManager.apk')
desired_caps['app-package'] = 'com.example.android.contactmanager'
desired_caps['app-activity'] = '.ContactManager'
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
el = driver.find_element_by_name("Add Contact")
el.click()
textfields = driver.find_elements_by_tag_name("textfield")
textfields[0].send_keys("My Name")
textfields[2].send_keys("")
driver.find_element_by_name("Save").click()
driver.quit()
请问楼主,下载ContactManager.apk所需要的密码是多少?
我也遇到了selenium.common.exceptions.WebDriverException: Message: u"A new session could not be created. (Original error: Parameter 'appPackage' is required for launching application)"
加这个群,群共享有ContactManager.apk
Parameter 'appPackage' is required for launching application怎么解决?
可以参考这个。是版本的问题
楼主你好!用代码编译后,textfields[0].send_keys("My Name")报index:error,请问怎么解决呀?试过很多方法了,自行解决不了,谢谢
from selenium import webdriver
Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(file), p)
desired_caps = {}
desired_caps['device'] = 'Android'
desired_caps['browserName'] = ''
desired_caps['version'] = '4.2.2'
desired_caps['app'] = PATH('C:\Users\Succi\Desktop\ContactManager.apk')
desired_caps['app-package'] = 'com.example.android.contactmanager'
desired_caps['app-activity'] = '.ContactManager'
driver = webdriver.Remote('', desired_caps)
el = driver.find_element_by_name("Add Contact")
el.click()
textfields = driver.find_elements_by_tag_name("textfield")
print("debug:",textfields)#input list
textfields[0].send_keys("My Name")
textfields[2].send_keys("")
driver.find_element_by_name("Save").click()
driver.quit()
错误信息:
('debug:', [])
Traceback (most recent call last):
File "android_contact.py", line 24, in
textfields[1].send_keys("My Name")
IndexError: list index out of range
当我用tag_name定位输入框的时候:textfields = driver.find_elements_by_tag_name("textfield")
为什么会提示定位错误呢?
错误信息:selenium.common.exceptions.WebDriverException: Message: u'Invalid locator strate gy: tag name‘
用xpath定位的时候,出现和楼上仁兄一样的问题,打印出来是空的!
我这边发现问题了,使用1.0版本的,会出现上述情况,使用0.14版本的,可以用tag name定位,奇怪了,1.0版本的怎么用呢?希望有高手回答下!多谢
from selenium import webdriver
from appium import webdriver两种方式有什么区别吗?
二python+selenium安装配置:第2、3、4步可以简化成下面一步到位
进入 直接下载ez_setup.py双击运行即可。安装成功后会发现在Python目录多了script文件夹。然后进入 第5步
desired_caps['app'] = PATH('C:\Users\Stephen\Desktop\ContactManager.apk')
是必须的吗?
如果不需要安装,直接运行机器里已经安装好的应用,要怎么处理?
请问下:运行以后出现:
raise URLError(err)
urllib2.URLError:
这个哪里出了问题呀,请指教
二python+selenium安装配置 第五步一直不行,求解啊
我已安装py3,然后按照你的步骤来 cmd 命令npm 来安装appium 报错,说不支持python3....有何解决方案呢...
楼主大大,这个apk怎么下载,云盘不能用了。可以给新地址不,谢谢
APK地址找不到了,能否重新提供一个?新手来混论坛,多多关照!
中提及了此贴
同样的错误请问怎样解决的?driver = webdriver.Remote('', desired_caps)这个东西是干嘛的?
[debug] [MJSONWP] Bad parameters: BadParametersError: Parameters were incorrect. We wanted {"required":["value"]} and you sent ["text","sessionId","id","value"]
请教下,运行之后报这个错误,不知道什么原因
后方可回复, 如果你还没有账号请点击这里 。
young (王阳)
第 666 位会员 /
灵动信息技术有限公司
共收到 34 条回复我通过id定位到EditText元素后,使用clear()方法清空内容,再用send_keys('str'),但在执行测试用例时,普通EditText内容是可以清空的正确输入内容,而密码EditText会把光标定位到文本第一位,追加输入send_keys('str')中的str
新人初次发帖,描述可能不太清楚,还望各位热心朋友指教
~Android吗?
可以将send_keys()方法封装一下...
加入driver.keyevent(123),这个方法会将光标移到最后,然后再执行clear()或send_keys(),方法有很多,多多尝试。
最后请加个头像。
def edittextclear(self,text):
self.driver.keyevent(123)
for i in range(0,len(text)):
self.driver.keyevent(67)
你好,首先非常感谢你的回复。
我按照你说明的方法找到以上方法,然后通过下方的代码使用
passEdit = self.driver.find.......获取控件
passEdit.click()
pwdtext2 = passEdit.get_attribute('text')
self.edittextclear(pwdtext2)
passEdit.send_keys('POMI12')
但仍然出现帖子里描述的问题
Andoid 有adb shell 命令输入文本的,可以尝试特殊输入替代sendKeys():
上面清除后,在CMD窗口先手动输入“adb shell input text "pwdtext2"如果可以你不妨封装一个 用这种方式封装一下
你好,“pwdtext2"是我要清除掉的文本内容,input不是输入么?
刚才我在pwdtext2 = passEdit.get_attribute('text')这行代码下面打印pwdtext2的type和len,输出类型为Unicode,长度为0,我想问一下是通过element.get_attribute('text')获取编辑框的文本内容对吧?
get_attribute是获取某个属性的值,这里获取的应该是编辑框中的文本内容。刚明白你的意思,我之前也碰到过类似情况,我在sendkeys()之前先做个Click()操作点击一下文本框,就解决了。sendkeys在输入之前自己会先Clear一下,所以Clear其实也不用加
按照你的方法试了也是不行,光标总会定位到文本前面
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: setText
info: [debug] [BOOTSTRAP] [debug] Using element passed in.
info: [debug] [BOOTSTRAP] [debug] Attempting to clear using UiObject.clearText().
info: [debug] [BOOTSTRAP] [debug] Text remains after clearing, but it appears to be hint text.
info: [debug] [BOOTSTRAP] [debug] Text not cleared. Assuming remainder is hint text.
info: [debug] [BOOTSTRAP] [debug] Sending plain text to element: 15.........(纯数字)
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
以上是普通编辑框clear()后再send_keys(‘15*********’)appium端输出的信息,这个可以成功清除文本且正确输入指定的文本
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: setText
info: [debug] [BOOTSTRAP] [debug] Using element passed in.
info: [debug] [BOOTSTRAP] [debug] Attempting to clear using UiObject.clearText().
info: [debug] [BOOTSTRAP] [debug] Sending plain text to element: ‘text_replace’
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
以上是密码编辑框clear()后再send_keys(‘text_replace’)appium端输出的信息,相比于普通编辑框输出的信息缺少了:
info: [debug] [BOOTSTRAP] [debug] Text remains after clearing, but it appears to be hint text.
info: [debug] [BOOTSTRAP] [debug] Text not cleared. Assuming remainder is hint text.
感觉出错应该是hint text的原因,但没能明白这个导致失败的原因也不知解决方法,还望指教
在此先感谢你能抽空回复我的疑问
补充一下调试设备信息 机型为红米1 Android版本为4.4.2
Text remains after clearing, but it appears to be hint text. //文本清除后有残留,但是显示的是隐藏文本
Text not cleared. Assuming remainder is hint text. //文本未被清除,假定其确实为隐藏文本
根据log信息,两种文本框在点击后处理方式是不一样的,下面的缺少那两行,原因应该是appium检测到 “文本已经被清除了”而不是向上面用隐藏的方式清除。我推测问题是出在你输入后,虽然发送了sendKey(),但是文本框未检测到你有输入,所以当输入结束后,提示信息又被显示出来了。
2.两个文本在手动操作时,点击文本框后,弹出来的键盘是否是相同的?输入密码的键盘是否是开发人员自己开发的。如果是内部开发的键盘,就需要你去点击键盘去输入了,而不能用sendKey()命令。
根据你的第一条回复,表示稍微能理解你的意思,但希望能指导下出现的原因
根据你的第二条回复,第一个编辑框弹出的是数字键盘,第二个编辑框弹出的普通的26字母那种键盘,且键盘非内部开发键盘
具体原因你可以咨询一下开发,我也不是太清楚。
你有尝试过用keyEvent()去输入密码吗?实在不行就点击对应键盘的坐标输入。
keyEvent()是可以的,但获取不了长度还是不太方便。
非常感谢你的热心回答
Try it..直接调这个方法清除试试。
def editClear(driver, el):
el.click()
driver.keyevent(123)
textLength = len(str(el.text))
for i in range(0, textLength):
driver.keyevent(67)
不好意思现在在回帖,这个方法之前已经是不行的,现在因为工作原因暂时没时间去研究解决方法,之后有遇到类似问题再研究下
后方可回复, 如果你还没有账号请点击这里 。
pomi (zhlog)
第 4423 位会员 /
共收到 12 条回复

我要回帖

更多关于 appium python教程 的文章

 

随机推荐