Appium 求解:在 OS X 系统上部署 Appium 环境,执行测试代码后,出现提示 “An element could not be located on the page using the given search parameters...”

pan · 2016年02月22日 · 最后由 pan 回复于 2016年04月01日 · 2540 次阅读

测试代码如下:

#coding=utf-8
from appium import webdriver
import time
'''
登陆
'''
desired_caps = {}
desired_caps['platformName'] = 'ios'
desired_caps['platformVersion'] = '8.4'
desired_caps['deviceName'] = 'iPhone Simulator'
desired_caps['app'] = ('/Users/yspan/Library/Developer/Xcode/DerivedData/eap-gnvyowbftuqxtxanwpnobrkvpmdy/Build/Products/Debug-iphonesimulator/eap.app')
desired_caps['bundleId'] = 'nanwang.csg.cn.iphone'
print("连接中...")
wd = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
print("连接完成!")
time.sleep(30)
wd.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATextField[1]/UIATextField[1]").send_keys("pan")
wd.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATextField[2]/UIATextField[1]").send_keys("15888888888")
wd.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATextField[3]/UIATextField[1]").send_keys("5555")
wd.find_element_by_name('登录').click()
print("success")
time.sleep(10)
wd.quit()

appium inspector 截图信息如下:

appium 日志如下:

info: [debug] Socket data being routed.
info: [debug] Got result from instruments: {"status":0,"value":{"UIAApplication":{"@":{"name":"幸福南网","label":"幸福南网","value":null,"dom":null,"enabled":true,"valid":true,"visible":true,"hint":null,"path":"/0","x":0,"y":24.125,"width":375,"height":642.1875},">":[{"UIAWindow":{"@":{"name":null,"label":null,"value":null,"dom":null,"enabled":true,

info: [debug] Pushing command to appium work queue: "au.getElementByIndexPath('/0/1/3/0')"

info: [debug] Sending command to instruments: au.getElementByIndexPath('/0/1/3/0')

info: [debug] [INST] 2016-02-22 06:40:51 +0000 Debug: Got new command 4 from instruments: au.getElementByIndexPath('/0/1/3/0')

info: [debug] [INST] 2016-02-22 06:40:51 +0000 Debug: evaluating au.getElementByIndexPath('/0/1/3/0')

info: [debug] [INST] 2016-02-22 06:40:51 +0000 Debug: evaluation finished
info: [debug] [INST] 2016-02-22 06:40:51 +0000 Debug: responding with:
info: [debug] [INST] 2016-02-22 06:40:51 +0000 Debug: Running system command #5: /Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/submodules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{"status":0,"value":""}...

info: [debug] Socket data received (25 bytes)

info: [debug] Socket data being routed.
info: [debug] Got result from instruments: {"status":0,"value":""}
info: [debug] Condition unmet after 2430ms. Timing out.

info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":""},"sessionId":"77424f61-8646-46bc-b9c9-4b3da01f385a"}
info: <-- POST /wd/hub/session/77424f61-8646-46bc-b9c9-4b3da01f385a/element 500 2434.032 ms - 179 

info: --> GET /wd/hub/status {}

info: [debug] Responding to client with success: {"status":0,"value":{"build":{"version":"1.4.13","revision":"c75d8adcb66a75818a542fe1891a34260c21f76a"},"isShuttingDown":false},"sessionId":"77424f61-8646-46bc-b9c9-4b3da01f385a"}

info: <-- GET /wd/hub/status 200 2.789 ms - 179 {"status":0,"value":{"build":{"version":"1.4.13","revision":"c75d8adcb66a75818a542fe1891a34260c21f76a"},"isShuttingDown":false},"sessionId":"77424f61-8646-46bc-b9c9-4b3da01f385a"}

info: --> GET /wd/hub/status {}

info: [debug] Responding to client with success: {"status":0,"value":{"build":{"version":"1.4.13","revision":"c75d8adcb66a75818a542fe1891a34260c21f76a"},"isShuttingDown":false},"sessionId":"77424f61-8646-46bc-b9c9-4b3da01f385a"}
info: <-- GET /wd/hub/status 200 1.779 ms - 179 {"status":0,"value":{"build":{"version":"1.4.13","revision":"c75d8adcb66a75818a542fe1891a34260c21f76a"},"isShuttingDown":false},"sessionId":"77424f61-8646-46bc-b9c9-4b3da01f385a"}

info: --> GET /wd/hub/status {}

info: [debug] Responding to client with success: {"status":0,"value":{"build":{"version":"1.4.13","revision":"c75d8adcb66a75818a542fe1891a34260c21f76a"},"isShuttingDown":false},"sessionId":"77424f61-8646-46bc-b9c9-4b3da01f385a"}

info: <-- GET /wd/hub/status 200 1.148 ms - 179 {"status":0,"value":{"build":{"version":"1.4.13","revision":"c75d8adcb66a75818a542fe1891a34260c21f76a"},"isShuttingDown":false},"sessionId":"77424f61-8646-46bc-b9c9-4b3da01f385a"}

终端日志如下:

连接中...
连接完成!
Traceback (most recent call last):
  File "/Users/yspan/Documents/shili/Test_login.py", line 21, in <module>
    wd.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATextField[3]/UIATextField[1]").send_keys("5555")
  File "/Library/Python/2.7/site-packages/selenium-2.52.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 258, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/Library/Python/2.7/site-packages/selenium-2.52.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 712, in find_element
    {'using': by, 'value': value})['value']
  File "/Library/Python/2.7/site-packages/selenium-2.52.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "build/bdist.macosx-10.11-intel/egg/appium/webdriver/errorhandler.py", line 29, in check_response
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

代码中,如果只是单独执行点击按钮的代码,是能正常执行的。文本输入内容部分的代码不能正常执行!

共收到 17 条回复 时间 点赞

有唯一 id 最好还是用 id 定位,出错 Log 也显而易见,感觉是写的 xpath 有问题,正常都是/[1]、/[2]、/[3],你的是 [1]/[1]、[2]/[1]、[3]/[1],还有最好将找控件和发送文本分开写:

a = wd.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATextField[1]/UIATextField[1]")
a.send_keys("pan")
pan #2 · 2016年02月23日 Author

#1 楼 @t880216t 谢谢你的提醒,那个 xpath 的路径是通过 inspector 上录制后复制出来的,应该不会有问题才对啊。

请问目前 Appium 支持 iOS 几了啊?谢谢

楼主,你有比较新的详细的,mac 上环境搭建及入门使用文档吗?谢谢

自己写 xpath 吧少年别复制黏贴。。。

#2 楼 @pys_moving 换别的定位方法试试

pan #7 · 2016年02月26日 Author

#5 楼 @mads 你的意思是说 inspector 上获取的 xpath 有可能不准确?自己写的话除了根据 inspector 上获取到元素的 xpath 写,还有其他方式麽?

pan #8 · 2016年02月26日 Author

#4 楼 @zhuquan0814 没有比较详细的环境搭建文档,都是一步一步百度搭建好的。即使能整理出文档,别人参照文档搭建时也有可能出现这样或者那样的问题,最后还是需要百度或者论坛找高手。

pan #9 · 2016年02月26日 Author

#6 楼 @neyo 你根据我提供的 appium inspector 截图信息,能提供一个元素定位方法麽?能根据 value 字段进行定位?

#9 楼 @pys_moving 昨天也有人问我这问题了,我拿我们的 app 看了下,怀疑是不是开发写控件写的不对。/UIATextField[3]/UIATextField[1] 这种,index 也是在上一层累加的,定位到上一层 uiatextfield[3] 可行吗。昨天那个小伙伴是通过点坐标来做的。。

#7 楼 @pys_moving 不知道你有没有做过 ui 自动化的 xpath 定位。遇到过很多只能自己去写的场景

pan #12 · 2016年03月01日 Author

#10 楼 @neyo 我先试试,到时再答复一下,实在不行只能使用坐标了。

pan #13 · 2016年03月01日 Author

#11 楼 @mads 使用 appium 做过安卓的测试,定位都挺顺利的,没有 IOS 这么折腾,可能是 IOS 开发上有不规范的问题。

#13 楼 @pys_moving 你在大网易?

pan #3 · 2016年03月04日 Author

#14 楼 @mads ?? 在网易工作? 不是啊!

pan #17 · 2016年04月01日 Author

问题已解决,暂不需要使用坐标这种极端的方式定位!这样即可:xm=wd.find_element_by_xpath("//UIATextField[@value='用户姓名']").send_keys("pan")。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册