Macaca 升级最新的 Macaca 后元素定位 element_by_name、element_by_xpath 无法定位元素

linpengcheng · 2017年05月12日 · 最后由 zz_hui 回复于 2017年11月24日 · 3525 次阅读

之前的用例通过 element_by_name 执行的,都可以跑通,但是在更新了 macaca 之后就不行了,通过 name 和 xpath 定位元素都定位不到。有谁也遇到这样的问题吗?
id 定位是可以的

通过 element_by_name 定位元素
self.driver.element_by_name('我的').click()
得到的报错为:

Traceback (most recent call last):
  File "/Users/test/Desktop/317hu_AppTest/Test_case/demo.py", line 68, in test1_peixun
    self.driver.element_by_name('我的').click()
  File "/usr/local/lib/python2.7/site-packages/macaca/util.py", line 77, in find_element
    return self.element(using.value, value)
  File "/usr/local/lib/python2.7/site-packages/macaca/webdriver.py", line 814, in element
    'value': value
  File "/usr/local/lib/python2.7/site-packages/macaca/webdriver.py", line 68, in _execute
    ret.raise_for_status()
  File "/usr/local/lib/python2.7/site-packages/macaca/webdriverresult.py", line 54, in raise_for_status
    raise WebDriverException(error, message, screen, stacktrace)
WebDriverException: 
Error: WebDriverError.INVALID_SELECTOR
Message: None

log 如下

>> responseHandler.js:11:12 [master] pid:8656 Recieve HTTP Request from Client[2017-05-12 13:31:54]: method: POST url: /wd/hub/session/628fa519-55d4-4d35-93b4-444e671638ca/element, jsonBody: {"using":"name","value":"我的"}
>> proxy.js:54:14 [master] pid:8656 Proxy: /wd/hub/session/628fa519-55d4-4d35-93b4-444e671638ca/element:POST to http://127.0.0.1:9001/wd/hub/session/628fa519-55d4-4d35-93b4-444e671638ca/element:POST with body: {"using":"name","value":"我的"}
>> proxy.js:82:20 [master] pid:8656 Got response with status 200: {"status":32,"value":"Argument was an invalid selector (e.g. XPath/CSS).","sessionId":null}
>> session.js:107:14 [master] pid:8656 Send HTTP Respone to Client[2017-05-12 13:31:54]: {"status":32,"value":"\"Argument was an invalid selector (e.g. XPath/CSS).\"","sessionId":null}
>> responseHandler.js:11:12 [master] pid:8656 Recieve HTTP Request from Client[2017-05-12 13:31:54]: method: DELETE url: /wd/hub/session/628fa519-55d4-4d35-93b4-444e671638ca, jsonBody: {}
>> session.js:80:12 [master] pid:8656 Delete session, sessionId: 628fa519-55d4-4d35-93b4-444e671638ca
>> responseHandler.js:47:14 [master] pid:8656 Send HTTP Respone to Client[2017-05-12 13:31:54]: {"sessionId":"628fa519-55d4-4d35-93b4-444e671638ca","status":0}

通过 element_by_xpath
elf.driver.element_by_xpath('//*[@text="我的"]').click()

得到的报错结果为

Traceback (most recent call last):
  File "/Users/test/Desktop/317hu_AppTest/Test_case/demo.py", line 69, in test1_peixun
    self.driver.element_by_xpath('//*[@text="我的"]').click()
  File "/usr/local/lib/python2.7/site-packages/macaca/util.py", line 77, in find_element
    return self.element(using.value, value)
  File "/usr/local/lib/python2.7/site-packages/macaca/webdriver.py", line 814, in element
    'value': value
  File "/usr/local/lib/python2.7/site-packages/macaca/webdriver.py", line 68, in _execute
    ret.raise_for_status()
  File "/usr/local/lib/python2.7/site-packages/macaca/webdriverresult.py", line 54, in raise_for_status
    raise WebDriverException(error, message, screen, stacktrace)
WebDriverException: 
Error: WebDriverError.INVALID_SELECTOR
Message: None

通 element_by_id 可正常执行
self.driver.element_by_id('id/home_mine_layout').click()

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 25 条回复 时间 点赞
proxy.js:82:20 [master] pid:8656 Got response with status 200: {"status":32,"value":"Argument was an invalid selector (e.g. XPath/CSS).","sessionId":null}
>> session.js:107:14 [master] pid:8656 Send HTTP Respone to Client[2017-05-12 13:31:54]: {"status":32,"value":"\"Argument was an invalid selector (e.g. XPath/CSS).\"","sessionId":null}

macaca 的 Bug?

这个是 bug,已经有人提出来了,估计正在解决~~~~

匿名 #4 · 2017年05月12日

升级后 wda 产生的 name 值是空的,估计是这个原因导致

升级新版驱动

已成功解决

@cynic 你是用 python 来做的吗?send_keys 有没有问题?

dustin 回复

有问题,send 不出来,只有一个数字。。。。

升级后还是找不到,我是通过 element_by_xpah 方法的,最后提示 NO such element,之前升级前可找到,升级后无法找到

驱动版本为 Installed driver list:
android: 2.0.9

macaca.webdriverexception.WebDriverException:
Error: WebDriverError.NO_SUCH_ELEMENT
Message: An element could not be located on the page using the given search parameters.

请问...这个问题修复了嘛...我跑 sample 好像还是跑不通 element_by_name 的部分...

lvyue 回复

一样。。可能又有问题了

那个...你跑通 element_by_xpath 的函数了么...我有的部分好像可以有的部分好像不行...刚学安卓...不是很懂...萌新疑惑....
另外,可以回退 android driver 的版本么...回退的话别的组件是不是会不匹配啊...这么多人用这个框架..总有解决办法的吧...?

lvyue 回复

xpath 貌似也有问题……有人说 xpath 不稳定。

lvyue 回复

之前是好的 但是我升级了最新的之后好像又不行了 element_by_name @xdf

>> responseHandler.js:47:14 [master] pid:11720 Send HTTP Respone to Client[2017-
06-15 22:10:02]: {"sessionId":"f66386bf-1cc7-4122-b6ae-ec937aa478a9","status":0,
"value":"{\"androidProcess\":\"com.tencent.mm:tools\",\"reuse\":\"3\",\"package\
":\"com.tencent.mm\",\"udid\":\"C4Y5T16810004018\",\"autoAcceptAlerts\":\"True\"
,\"activity\":\".ui.LauncherUI\",\"platformName\":\"Android\"}"}
>> responseHandler.js:11:12 [master] pid:11720 Recieve HTTP Request from Client[
2017-06-15 22:10:07]: method: POST url: /wd/hub/session/f66386bf-1cc7-4122-b6ae-
ec937aa478a9/element, jsonBody: {"using":"name","value":"微信"}
>> proxy.js:55:14 [master] pid:11720 Proxy: /wd/hub/session/:sessionId/accept_al
ert:POST to http://127.0.0.1:9005/wd/hub/session/:sessionId/accept_alert:POST wi
th body: {}
>> proxy.js:83:20 [master] pid:11720 Got response with status 200: {"status":0,"
sessionId":null}
>> proxy.js:55:14 [master] pid:11720 Proxy: /wd/hub/session/f66386bf-1cc7-4122-b
6ae-ec937aa478a9/element:POST to http://127.0.0.1:9005/wd/hub/session/:sessionId
/element:POST with body: {"using":"name","value":"微信"}
>> proxy.js:83:20 [master] pid:11720 Got response with status 200: {"status":32,
"value":"Argument was an invalid selector (e.g. XPath/CSS).","sessionId":"f66386
bf-1cc7-4122-b6ae-ec937aa478a9"}
>> session.js:107:14 [master] pid:11720 Send HTTP Respone to Client[2017-06-15 2
2:10:07]: {"status":32,"value":"\"Argument was an invalid selector (e.g. XPath/C
SS).\"","sessionId":"f66386bf-1cc7-4122-b6ae-ec937aa478a9"}
>> responseHandler.js:11:12 [master] pid:11720 Recieve HTTP Request from Client[
2017-06-15 22:10:07]: method: DELETE url: /wd/hub/session/f66386bf-1cc7-4122-b6a
e-ec937aa478a9, jsonBody: {}
>> session.js:80:12 [master] pid:11720 Delete session, sessionId: f66386bf-1cc7-
4122-b6ae-ec937aa478a9
>> responseHandler.js:47:14 [master] pid:11720 Send HTTP Respone to Client[2017-
06-15 22:10:07]: {"sessionId":"f66386bf-1cc7-4122-b6ae-ec937aa478a9","status":0}

@xdf 麻烦尽早处理这个问题吧。。。

dustin 回复

问题已经修复。麻烦升级最新的 macaca-android

npm install macaca-android@2.0.23 -g

造成困扰,请谅解。

问题已经修复。麻烦升级最新的 macaca-android

npm install macaca-android@2.0.23 -g

问题已经修复。麻烦升级最新的 macaca-android

npm install macaca-android@2.0.23 -g

童华兵 回复

好的,谢谢。。。。大家一起努力把这个项目做好!😃

安装的 macaca-android@2.0.40 ,找到元素以后的 getText() 和 click() 都不行。
报错信息:An unknown server-side error occurred while processing the command.有遇到这个情况么

具体 log:

proxy.js:85:20 [master] pid:65454 Got response with status 200: {"status":0,"value":{"ELEMENT":"1"},"sessionId":"594f3b0f-a4ce-4208-b083-4e4ecf71aa8b"}
session.js:109:14 [master] pid:65454 Send HTTP Respone to Client[2017-11-24 11:40:23]: {"status":0,"value":"{\"ELEMENT\":\"1\"}","sessionId":"594f3b0f-a4ce-4208-b083-4e4ecf71aa8b"}
responseHandler.js:11:12 [master] pid:65454 Recieve HTTP Request from Client[2017-11-24 11:40:23]: method: GET url: /wd/hub/session/594f3b0f-a4ce-4208-b083-4e4ecf71aa8b/element/1/text, jsonBody: {}
restart UIAutomatorWD server
uiautomator-client.js:61:14 [master] pid:65454 INSTRUMENTATION_RESULT: shortMsg=Process crashed.
INSTRUMENTATION_CODE: 0
uiautomator-client.js:61:14 [master] pid:65454

uiautomator-client.js:61:14 [master] pid:65454 INSTRUMENTATION_STATUS: numtests=1
uiautomator-client.js:61:14 [master] pid:65454

uiautomator-client.js:61:14 [master] pid:65454 INSTRUMENTATION_STATUS: stream=
uiautomator-client.js:61:14 [master] pid:65454
com.macaca.android.testing.UIAutomatorWD:
INSTRUMENTATION_STATUS: id=AndroidJUnitRunner

uiautomator-client.js:61:14 [master] pid:65454 INSTRUMENTATION_STATUS: test=MacacaTestRunner
uiautomator-client.js:61:14 [master] pid:65454

uiautomator-client.js:61:14 [master] pid:65454 INSTRUMENTATION_STATUS: class=com.macaca.android.testing.UIAutomatorWD
INSTRUMENTATION_STATUS: current=1

uiautomator-client.js:61:14 [master] pid:65454 INSTRUMENTATION_STATUS_CODE: 1
uiautomator-client.js:61:14 [master] pid:65454

uiautomator-client.js:61:14 [master] pid:65454 INSTRUMENTATION_STATUS: stream=
uiautomator-client.js:61:14 [master] pid:65454
UIAutomatorWD->http://localhost:62348<-UIAutomatorWD
INSTRUMENTATION_STATUS_CODE: 0

UIAutomatorWD http server ready
proxy.js:55:14 [master] pid:65454 Proxy: /wd/hub/session/:sessionId/accept_alert:POST to http://127.0.0.1:62348/wd/hub/session/:sessionId/accept_alert:POST with body: {}
proxy.js:85:20 [master] pid:65454 Got response with status 200: {"status":0,"sessionId":":sessionId"}
restart UIAutomatorWD server
uiautomator-client.js:61:14 [master] pid:65454 INSTRUMENTATION_RESULT: shortMsg=Process crashed.

uiautomator-client.js:61:14 [master] pid:65454 INSTRUMENTATION_CODE: 0

uiautomator-client.js:61:14 [master] pid:65454 INSTRUMENTATION_STATUS: numtests=1

uiautomator-client.js:61:14 [master] pid:65454 INSTRUMENTATION_STATUS: stream=
com.macaca.android.testing.UIAutomatorWD:
INSTRUMENTATION_STATUS: id=AndroidJUnitRunner
INSTRUMENTATION_STATUS: test=MacacaTestRunner
INSTRUMENTATION_STATUS: class=com.macaca.android.testing.UIAutomatorWD

uiautomator-client.js:61:14 [master] pid:65454 INSTRUMENTATION_STATUS: current=1
INSTRUMENTATION_STATUS_CODE: 1

uiautomator-client.js:61:14 [master] pid:65454 INSTRUMENTATION_STATUS: stream=
UIAutomatorWD->http://localhost:62348<-UIAutomatorWD
UIAutomatorWD http server ready
proxy.js:55:14 [master] pid:65454 Proxy: /wd/hub/session/594f3b0f-a4ce-4208-b083-4e4ecf71aa8b/element/1/text:GET to http://127.0.0.1:62348/wd/hub/session/:sessionId/element/1/text:GET with body: {}
uiautomator-client.js:61:14 [master] pid:65454
INSTRUMENTATION_STATUS_CODE: 0

proxy.js:85:20 [master] pid:65454 Got response with status 200: {"status":13,"value":"An unknown server-side error occurred while processing the command.","sessionId":"594f3b0f-a4ce-4208-b083-4e4ecf71aa8b"}
session.js:109:14 [master] pid:65454 Send HTTP Respone to Client[2017-11-24 11:40:26]: {"status":13,"value":"\"An unknown server-side error occurred while processing the command.\"","sessionId":"594f3b0f-a4ce-4208-b083-4e4ecf71aa8b"}

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