Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Appium+;安卓&x2B;WebDriver findElement():在sendKeys()之后找不到元素?_Android_Selenium_Appium - Fatal编程技术网

Android Appium+;安卓&x2B;WebDriver findElement():在sendKeys()之后找不到元素?

Android Appium+;安卓&x2B;WebDriver findElement():在sendKeys()之后找不到元素?,android,selenium,appium,Android,Selenium,Appium,我有一个模拟的Android设备和Appium。我的测试在特定文本字段中成功启动了正确的活动和类型。但是,当我试图查找相同的文本字段以检查其中的文本时,我得到了“使用给定的搜索参数无法在页面上找到某个元素。”即使我尝试重新使用该元素而不是第二次搜索它,它仍然会失败,并显示相同的消息。我应该怎么做?也许第二个findElement()的上下文是错误的——我也找不到文本字段旁边的按钮 这是一个git回购,其中包含一个应用程序和一个测试项目。失败的JUnit测试演示了以下问题: 以下详细信息(代码和应

我有一个模拟的Android设备和Appium。我的测试在特定文本字段中成功启动了正确的活动和类型。但是,当我试图查找相同的文本字段以检查其中的文本时,我得到了
“使用给定的搜索参数无法在页面上找到某个元素。”
即使我尝试重新使用该元素而不是第二次搜索它,它仍然会失败,并显示相同的消息。我应该怎么做?也许第二个
findElement()
的上下文是错误的——我也找不到文本字段旁边的按钮

这是一个git回购,其中包含一个应用程序和一个测试项目。失败的JUnit测试演示了以下问题:

以下详细信息(代码和应用日志交错)

这是第一个成功的发现。活动的布局xml文件对我要查找的文本字段具有以下属性:
android:id=“@+id/edit_message”

第一次findElement成功:

debug: Appium request initiated at /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element
debug: Request received with params: {"using":"id","value":"edit_message"}
info: Pushing command to appium work queue: ["find",{"strategy":"id","selector":"edit_message","context":"","multiple":false}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"edit_message","context":"","multiple":false}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: find
info: [BOOTSTRAP] [debug] Finding edit_message using ID with the contextId: 
info: [BOOTSTRAP] [info] Returning result: {"value":{"ELEMENT":"1"},"status":0}
info: Responding to client with success: {"status":0,"value":{"ELEMENT":"1"},"sessionId":"0ec259be-87e0-47f6-9279-da577fe29a07"}
POST /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element 200 5656ms - 109b
debug: Appium request initiated at /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element/1/value
debug: Request received with params: {"id":"1","value":["hello!"]}
info: Pushing command to appium work queue: ["element:setText",{"elementId":"1","text":"hello!"}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"element:setText","params":{"elementId":"1","text":"hello!"}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: setText
info: [BOOTSTRAP] [info] Returning result: {"value":true,"status":0}
info: Responding to client with success: {"status":0,"value":true,"sessionId":"0ec259be-87e0-47f6-9279-da577fe29a07"}
POST /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element/1/value 200 4215ms - 89b
打招呼

String text = "hello!";
e.sendKeys(text);
这将成功:

debug: Appium request initiated at /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element
debug: Request received with params: {"using":"id","value":"edit_message"}
info: Pushing command to appium work queue: ["find",{"strategy":"id","selector":"edit_message","context":"","multiple":false}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"edit_message","context":"","multiple":false}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: find
info: [BOOTSTRAP] [debug] Finding edit_message using ID with the contextId: 
info: [BOOTSTRAP] [info] Returning result: {"value":{"ELEMENT":"1"},"status":0}
info: Responding to client with success: {"status":0,"value":{"ELEMENT":"1"},"sessionId":"0ec259be-87e0-47f6-9279-da577fe29a07"}
POST /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element 200 5656ms - 109b
debug: Appium request initiated at /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element/1/value
debug: Request received with params: {"id":"1","value":["hello!"]}
info: Pushing command to appium work queue: ["element:setText",{"elementId":"1","text":"hello!"}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"element:setText","params":{"elementId":"1","text":"hello!"}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: setText
info: [BOOTSTRAP] [info] Returning result: {"value":true,"status":0}
info: Responding to client with success: {"status":0,"value":true,"sessionId":"0ec259be-87e0-47f6-9279-da577fe29a07"}
POST /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element/1/value 200 4215ms - 89b
这是第二个失败的结果。(如果我跳过这个findElement并重新使用原来的findElement,或者如果我试图找到文本字段旁边的Send按钮,我仍然会遇到类似的失败)

以下是故障日志:

debug: Appium request initiated at /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element
debug: Request received with params: {"using":"id","value":"edit_message"}
info: Pushing command to appium work queue: ["find",{"strategy":"id","selector":"edit_message","context":"","multiple":false}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"edit_message","context":"","multiple":false}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: find
info: [BOOTSTRAP] [debug] Finding edit_message using ID with the contextId: 
info: [BOOTSTRAP] [info] Returning result: {"value":"No element found","status":7}
info: Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"No element found"},"sessionId":"0ec259be-87e0-47f6-9279-da577fe29a07"}
POST /wd/hub/session/0ec259be-87e0-47f6-9279-da577fe29a07/element 500 874ms - 223b
有一个HTML的请求。我正在测试一个本地Android应用程序。下面是当前测试活动的布局xml。如果还有什么我应该包括在这里,请告诉我

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />
<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/button_send"
   android:onClick="sendMessage" android:id="@+id/send"/>
</LinearLayout>

您应该尝试sdk附带的android inspector。它不仅可以提供树中确切元素的id,还可以提供许多其他细节。它没有xPath,因此您可能仍然需要使用appium检查器,具体取决于您想做什么。请在

./path-to-sdk/android-sdk-macosx/tools/uiautomatorviewer

输入文本后检查元素,并确保ID在输入后未发生变化。使用的ID不应是
“edit_message”
,而应是
“com.company.app:ID/edit_message”

基本上,如果layout.xml文件使用
android:ID=“@+ID/foo”
(并且你的应用程序是com.company.app),为你的UI元素指定了一个ID,那么在测试中你应该使用
“com.company.app:ID/foo”
,而不仅仅是
“foo”


仅使用
“foo”
可以找到UI元素,但与之交互一次后,您将无法在UI中找到任何内容。不知道为什么会这样,但这会使事情变得混乱。

让一些线程睡眠。这是因为您的应用程序活动需要一些时间才能启动。在此之前,UI自动机无法找到元素。您可以在发送密钥之前设置线程休眠

除非您向我们显示html,否则我们无法帮助您。根据此消息-没有id为
edit\u message
的元素添加了布局xml。有一个元素id为edit_message,但我无法在第二次找到它(或其他任何东西)。您确定
@+id/edit_message
没有在该id前面加上某些东西吗?像
123/edit_message
?我不知道如何在应用程序运行时检查它(与对网页使用firebug相比)。但它确实第一次找到了元素。您是否建议在
sendKeys()
之后更改id?因为我对android开发和appium一无所知,所以我在这方面没有什么用处,但作为selenium的贡献者和用户,我可以在这方面提供帮助。似乎那个身份证不在那里。appium对id所做的任何事情都是我无法控制的。
uiautomatorviewer
只要我的测试没有运行,就可以正常工作。如果查看器正在运行,然后我启动测试(或者如果我启动测试,然后启动查看器),查看器会说
原因:获取UI层次结构XML文件时出错:com.android.ddmlib.SyncException:远程对象不存在。。。。如果我使用查看器并手动键入文本字段,元素ID将保持与预期相同:
com.company.app:ID/edit_message
还有另一个可用的工具称为“hierarchyviewer”。这应该能够获得层次结构,但可能无法获得图像。您的应用程序太大,无法用于该工具。我的应用程序也有同样的问题,因为开发人员在使用它之后更新了它,我注意到它也被弃用了。。。。尝试android sdk/tools/Monitor如何检查元素?测试运行时uiautomatorviewer不工作(请参阅我对user2984233的评论)。我在上面的问题中添加了一个回购的链接,该链接复制了该问题(至少在我的设置中)