android – Can we find element by ID in appium-ThrowExceptions

Exception or error:

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

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)

i tried following code:

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

where txtLogin is id for login button

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

How to solve:

You can use element ID as following:-

package name : com.example.testap

Element ID : txtLogin

write the following code –

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

###

You can find by id using Xpath too,e.g.
enter image description here
here element has class=”android.widget.Button” and id=”digit5″ so you can find like

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

Xpath is most useful when you want apply Multiple condition like
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

###

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

//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

###

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.

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.

###

In the Appium version 1.0 onwards, findElementByTagName is deprecated.

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

findElements(By.className(“android.widget.EditText”));

findElements(By.className(“android.widget.Button”));

###

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.

enter image description here

Leave a Reply

Your email address will not be published. Required fields are marked *