当前位置: 首页 > 知识库问答 >
问题:

Appium+Android+WebDriver findElement():在sendKeys()之后找不到元素?

单嘉泽
2023-03-14

这里有一个git repo包含一个应用程序和一个测试项目。失败的JUnit测试证明了问题:https://github.com/achengs/an-appium-question

详细信息如下(代码和Appium日志交错)

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

public static final String MESSAGE_TO_SEND = "edit_message";
...
WebElement e = driver.findElement(By.id(MESSAGE_TO_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":{"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
String text = "hello!";
e.sendKeys(text);
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
WebElement f = driver.findElement(By.id(MESSAGE_TO_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>

共有1个答案

姬乐
2023-03-14

使用的ID不应是“edit_message”而是“com.company.app:ID/edit_message”

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

只使用“foo”可能允许找到UI元素,但在与它进行一次交互之后,您将无法在UI中找到任何内容。不知道这是为什么,但它混淆了事情。

 类似资料:
  • 我写了测试自动化脚本为Android使用selenium和AppiumJava作为编码语言。脚本在Android 6模拟器上工作得非常好。但是,在Android 7模拟器上,驱动程序启动应用程序,但无法找到应用程序上的任何元素。 示例代码: 其中android.widget.TextView是类名,文本属性包含作为文本的设备。 当我运行这个时,它在驱动程序处失败。findElement步骤并抛出N

  • 自动化测试一直持续到最近(大约一个月前)。问题在于,在使用Appium inspect element工具以及在IOS上运行测试时,无法找到元素。 辅助功能ID选择器显示在Appium检查工具中,但是当您尝试使用相同的选择器搜索元素时,找不到元素。 注意:在Android上为相同的元素使用内容文件可以正常工作。 知道如何解决这个问题吗? 依赖关系: 应用依赖:

  • 我是新来的。如果问题重复,很抱歉,但我没有找到解决方法。 我有一个问题--我无法使用Xpath定位元素,但我使用ID定位器定位它没有问题。见附件截图。这一问题不仅针对这一要素,而且也针对所有要素。看来Xpath根本不起作用。 我正在使用Appium 7.3.0(试用过不同版本),UIAutomator2,方法是在模拟器上设置,Android 8.1功能。 null 此外,我在需要的地方使用了和,因

  • 我正在使用appium来自动化android应用程序。在这种情况下,无法对文本字段执行sendkeys():单击Add Customer选项。翻开了新的一页。我正在尝试在文本字段中输入值。我能够使用XPath找到页面上的文本字段。我可以点击文本字段。但是当我执行sendkeys()时,它失败了。参考下面的截图。 链接:2-添加客户-打开新页面(第2页)并尝试输入详细信息

  • 即使其中一个软断言失败,测试也会继续。但是在我的例子中,我的测试在软断言失败后停止,因为找不到下一个测试元素。 这可以正常工作,但如果我设置: 我收到以下错误消息: 组织。openqa。硒。无接触元素异常。第二个软断言!!! 测试停止。 环境:Selenium 3.5 geckodriver Mozilla 56.0.1。

  • 我正在尝试创建一个测试,在这个测试中,我必须在iframe中填写一些信息。访问iframe工作正常,我可以在框架内填写信息。问题是,当我找到一个元素“a”时,它会附加一个回发,它会在iframe中重新加载内容,以找到另一个元素“B”。所以我找不到那个元素。我遇到以下错误: 以下是我的观察:当我第一次找到iframe时,它看起来像这样: 发生回发后,如下所示: 我可以很容易地找到元素A: 但是元素B