【问题标题】:Can we find element by ID in appium我们可以在appium中通过ID找到元素吗
【发布时间】:2014-04-04 05:35:25
【问题描述】:

以下链接提到我们可以通过提供 id 找到元素...但我无法找到它。

https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/finding-elements.md

按“名称”查找(即文本、标签或开发人员生成的 ID,也就是元素的“accessibilityIdentifier”)

我尝试了以下代码:

WebElement el = driver.findElement(By.name("txtLogin"));

其中 txtLogin 是登录按钮的 ID

它给出了以下异常:

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)

请任何人解释在 appium 中查找元素的所有方法

【问题讨论】:

    标签: android appium


    【解决方案1】:

    您可以使用以下元素 ID:-

    包名:com.example.testap

    元素 ID:txtLogin

    编写以下代码-

     driver.findElement(By.id("com.example.testapp:id/txtLogin")).sendKeys("abc");
    

    【讨论】:

    • 这是否仅适用于以上android版本> = 18?
    • 如果您的按钮属于另一个未在所需功能中使用的应用程序,那么您应该使用 id 及其包名。所以在您的情况下 driver.findElement(By.id("txtLogin") ).sendKeys("abc");会工作的。
    【解决方案2】:

    这里是使用 Appium 定位元素的最常用方法的 java 代码

    //Find element by id and enter Hello in Text box.
    
        driver.findElementById("edit_name").sendKeys("Hello");
        //Find element by class name and enter Hello in Text box.
    
         driver.findElementByClassName("com.android.EditText").sendKeys("Hello");
        //Find element by xpath and enter Hello in Text box.
    
        driver.findElementByXPath("//cass[@value='Enter Name']").sendKeys("Hello");
        //Find element by link text and enter Hello in Text box.
    
        driver.findElementByLinkText("Enter Name").sendKeys("Hello");
    

    如果您想了解更多在 appium 中为原生和 web 视图查找元素的方法VISIT HERE

    【讨论】:

      【解决方案3】:

      您也可以使用 Xpath 按 id 查找,例如 这里元素有 class="android.widget.Button"id="digit5" 所以你可以找到像

      xpath("//android.widget.Button[contains(@resource-id,'digit5')]")

      Xpath 在您想要应用 多个 条件 时最有用 xpath("//android.widget.Button[contains(@resource-id,'digit5') and @text='5']") 更多详细信息,您可以在http://www.software-testing-tutorials-automation.com/2015/10/ui-automator-viewer-get-android-app.html 上查看

      【讨论】:

        【解决方案4】:

        对于低于 18 的 Android API 级别,无法通过 id 获取元素。如果 id 存在,您能否在 uiautomatorviewer 中检查。如果没有,您可以获取 EditView 类型的所有元素并对其进行迭代以找到正确的字段以发送文本。

        这样使用 -

        public List<WebElement> getWebElementList(By by) {
        
                return driver.findElements(by);
            }
        
        public void details() throws InterruptedException{
        
                List<WebElement> weList = null;
                weList = getWebElementList(By.tagName("android.widget.EditView"));
                for (WebElement we : weList) {
                    if(we.getText().toLowerCase().contains("login")){
                        we.sendkeys("Hello");
                    }
                }           
            }
        

        希望这会有所帮助。

        【讨论】:

          【解决方案5】:

          在 Appium 1.0 版以后,不推荐使用 findElementByTagName。

          您必须将 findElementByClassName 与合格的类名一起使用。 android中EditTextField和Button的代码如下:

          findElements(By.className("android.widget.EditText"));

          findElements(By.className("android.widget.Button"));

          【讨论】:

            【解决方案6】:

            您可以使用 android uiautomatorviewer 找到元素的 ID。 只需转到您的 SDK 工具文件夹 D:\Android\android-sdk\tools,在这里您可以找到 uiautomatorviewer。 打开它并截取您的活动的屏幕截图,然后找到任何元素的 id、类、包。

            【讨论】:

            • resource-id是id
            猜你喜欢
            • 2016-05-30
            • 2020-08-11
            • 1970-01-01
            • 2017-09-14
            • 2013-07-23
            • 2020-11-25
            • 2017-04-24
            • 2013-02-06
            • 1970-01-01
            相关资源
            最近更新 更多