22

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

Tejasvi Manmatha
  • 564
  • 7
  • 12
nilesh
  • 1,483
  • 2
  • 19
  • 37

6 Answers6

24

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");
Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
Mukul
  • 256
  • 2
  • 3
  • Is this only valid for above android version >= 18 ? – Shobhit Puri Jul 22 '15 at 19:52
  • 1
    If your button belongs to another app which is not used in desired capabilities, then you should use id with its package name.. So in your case driver.findElement(By.id("txtLogin")).sendKeys("abc"); will work. – Chandrashekhar Swami Jan 13 '16 at 11:47
6

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

anuja jain
  • 1,367
  • 13
  • 19
5

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

Shreekant N
  • 858
  • 1
  • 13
  • 28
1

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.

user2220762
  • 565
  • 4
  • 16
1

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

Rams_QA
  • 121
  • 7
1

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

Poras Bhardwaj
  • 1,073
  • 1
  • 15
  • 33