Android 我们可以在appium中通过ID查找元素吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22142219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:33:14  来源:igfitidea点击:

Can we find element by ID in appium

androidappium

提问by nilesh

following link mentions that we can find element by giving id... but i am unable to find it.

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

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

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

find by "name" (i.e., the text, label, or developer-generated ID a.k.a 'accessibilityIdentifier' of an element)

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

i tried following code:

我尝试了以下代码:

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

where txtLogin is id for login button

其中 txtLogin 是登录按钮的 ID

it gives following exception:

它给出了以下异常:

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)

can please anyone explain what are all the ways there to find elements in appium

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

回答by Mukul

You can use element ID as following:-

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

package name : com.example.testap

包裹名字 : com.example.testap

Element ID : txtLogin

元素 ID : txtLogin

write the following code -

编写以下代码 -

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

回答by Shreekant N

You can find by id using Xpathtoo,e.g. enter image description herehere element has class="android.widget.Button"and id="digit5"so you can find like

您也可以使用Xpath通过 id 查找,例如 在此处输入图片说明这里元素有class="android.widget.Button"id="digit5"所以你可以找到像

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

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

Xpath is most useful when you want apply Multipleconditionlike xpath("//android.widget.Button[contains(@resource-id,'digit5') and @text='5']")for more details you can see on http://www.software-testing-tutorials-automation.com/2015/10/ui-automator-viewer-get-android-app.html

当您想要应用多个条件时,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

回答by anuja jain

Here is the jave code with most common ways to locate elements using Appium

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

//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");

If you want to learn more ways to find elements in appium for native and webviews VISIT HERE

如果你想了解更多在 appium 中为原生和 webviews 查找元素的方法,请访问这里

回答by user2220762

Getting element by id is not possible for Android API level less than 18.Can you check in the uiautomatorviewer if the id is present. If not you can get all the elements of the type EditView and iterate over it to find the correct field to send text.

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

Use it this way -

用这种方式——

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");
            }
        }           
    }

Hope this helps.

希望这可以帮助。

回答by Rams_QA

In the Appium version 1.0 onwards, findElementByTagName is deprecated.

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

You have to use the findElementByClassName instead with qualified class Name. Code for EditTextField and Button in android as below :

您必须使用 findElementByClassName 而不是限定的类名称。android 中 EditTextField 和 Button 的代码如下:

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

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

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

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

回答by Poras Bhardwaj

You can find the ID of an element using android uiautomatorviewer. just go to your SDK tools folder D:\Android\android-sdk\tools, here you can find uiautomatorviewer. Open it and take screen shots of your activity and find the id, class, package of any element.

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

enter image description here

在此处输入图片说明