Skip to main content

FindElement and FindElements Commands

The next thing to do is to interact with the different elements of the application and for that it is required to locate elements first before interacting. Locating elements can be done on the AndroidDriver instance itself, as it gives us ‘Find Elementand ‘Find Elements method to locate elements on the Appium. In this chapter we will learn to use FindElement andFindElements commands or how to locate elements with Appium UiAutomatorViewerAppium Inspector with different attributes.

How to Use FindElement & FindElements Method in Appium

The difference between Find Element and Find Elements method is the first returns a WebElement object otherwise it throws an exception and the latter returns a list of WebElements, it can return an empty list if no DOM elements match the query. The Find methods take a locator or query object called By. ‘By’ strategies are listed below.

By Name

This is also an efficient way to locate an element but again the problem is same as with ID that UI developer make it having non-unique names on a page or auto-generating the names. With this strategy, the first element with the name attribute value matching the location will be returned. If no element has a matching name attribute, a NoSuchElementExceptionwill be raised.
Example 1: If an element is given like this:
UIAutomatorViewer_2_2
The above element can be located with the below commands:

Example 2: If an element is given like this:
UIAutomatorViewer_4
The above element can be located with the below commands:

How to find element using ‘content-desc‘ attribute in Appium?

content-desc attribute can be used exactly like text attribute with findElementByName. Look at the example below:
Example 3: If an element is given like this:
UIAutomatorViewer_5
The above element can be located with the below commands:

By Class Name

With this you can find elements based on the value of the class attribute. If an element has many classes then this will match against each of them. A class can contain many elements.
Example: If an element is given like this:
UIAutomatorViewer_3
The above element can be located with the below commands:
Note: This will only work efficiently when this is a unique class name, which is generally not a case in Appium. But still we can use the className in conjunction with other, we will have a detailed example below for it.

By ID

With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a NoSuchElementException will be raised. This is the most efficient and preferred way to locate an element, as most of the times IDs are unique.
Example: If an element is given like this:
UIAutomatorViewer_2
The above element can be located with the below commands:

By XPath

Xpath is a true blessing in Appium, as you will end up many situation where you do not have any options but to use xpath.Xpath can be relative and absolute but it is suggested to always use relative xpath.
Let’s look at the example below for Password field of Amazon Sign In module. Password text box does not have any IDattached to it, not any text value and content-desc. But in this case we have className populated but again that is not help, as the className of the UserName text box is also same.
Example: If an element is given like this
UIAutomatorViewer_7
If you inspect ‘(5) View{Password} […][…]‘ you will notice that the element got content-desc value as ‘Password‘. It means we can easily locate the (5)element with the attribute content-desc and then traverse down to (6)element and with that we can easily reach out to its child element EditText.
To locate the (5)element, xpath can be used:

How to use Siblings in Xpath

But our scenario says that we need to locate child of the (6)element. Hence first it is required to locate (6) which is sibling of (5). In Xpath, there is a way to locate siblings elements. Below statement will locate the (6)element:
Once we reach to (6)element, it is easy to locate the Password text field which is child of (6) and that can be done with this:

A complete code would look like this now:


How to find Element using Parent node?

This is again a very important technique to locate element in Appium. There will be many situations where the only option to locate element is to get the parent element first and then get its child object to perform any action on child element.
Consider Username text box for this scenario. UserName text box does not have any ID attached to it, not any text value and content-desc. But in this case we have className populated but again that is not help, as the className of thePassword text box is also same as ‘android.widget.EditText‘. If we go one step up to the parent of EditText, it is View with theclassName as ‘android.view.View‘ which is again not unique. So, we need to go one more step up for finding unique parent, which is Amazon Sign In shown in the below image:
UIAutomatorViewer_7_1
The above element can be located with the below commands:
With using above statement, we would be able to get the parent element, but we are interested in the child elements of the parent. With the below statement, we will get all the child elements ‘Views‘ which is under selected parent:
Now we have all the Views element with us in childElements, but we are interested in the 5th child element, as usernametext box is under that 5th View element. See the below screenshot for help.
UIAutomatorViewer_6
The above element can be located with the below commands:
Note: We have mention .get(4) for 5th element because the index starts from zero.
Once we get the main element(5th), it is easy to point it’s child element:
A complete code would look like this now:


Practice Exercise

1) Start Appium Node Server
2) Launch Amazon App
3) Click on ‘Shop By Department’ using By.ID.
4) Click on Menu button using By.ClassName to display the menu list.
5) Click on Home link using By.Name to navigate back to the Home Screen.
6) Click on the SIgn In link using By.Name to Log In in to the application.
7) Enter text in the UserName field using By.xpath & Sibling strategy
8) Enter text in the Password field using Parent node strategy
9) Click on the Submit button using content-desc attribute.

Solution

Comments

Popular posts from this blog

JMeter Exceeded Maximum Number of Redirects Error Solution

While running performance test, JMeter allows maximum 5 redirects by default. However, if your system demands more than 5 redirects, it may result in JMeter exceeded maximum number of redirects error. In this post, we have listed down steps to overcome this error. Actual error in JMeter: Response code: “Non HTTP response code: java.io.IOException” Response message: “Non HTTP response message: Exceeded maximum number of redirects: 5” This error is noticed because  JMeter  allows maximum 5 redirects by default and your system may be using more than 5 redirects. You need to increase this count to more than 5 in jmeter.properties file. Follow below steps to achieve this. Navigate to /bin directory of your JMeter installation. Locate jmeter.properties file and open it in any editor. Search for “httpsampler.max_redirects” property in opened file. Uncomment the above property by removing # before it. Change to value to more than 5 Eg. 20. Save the file and restart JMet...

SSO with SAML login scenario in JMeter

SAML(Security Assertion Markup Language) is increasingly being used to perform single sign-on(SSO) operations. As WikiPedia puts it, SAML is an XML-based open standard data format for exchanging authentication and authorization data between parties, in particular, between an identity provider and a service provider. With the rise in use of SAML in web applications, we may need to handle this in JMeter. This step-by-step tutorial shows SAML JMeter scenario to perform login operation. First request from JMeter is a GET request to fetch Login page. We need to fetch two values ‘SAMLRequest’ and ‘RelayState’ from the Login page response data. We can do this by using  Regular Expression Extractor . These two values need to be sent in POST request to service provider. Refer below image to see how to do this. We will get an HTML login page as a response to the request sent in 1st step. We need to fetch values of some hidden elements to pass it in the next request. We...

VBScript Code - Function to convert CSV file into excel and viceversa in QTP using VBScript

We at times are required to convert excel files into csv to read as flat files and sometime require to convert a csv file into excel file to use excel features on the data.   Below function shows how to convert an csv file into excel file and vice versa. We can also convert to other formats based on constants Here constant value 23 is used to create a csv file and constant -4143 to save a file as xls file. Once the destination file is created, we can delete the source file as shown below.  In case of any issue in understanding the code, please add in comment section Call func_ConversionCSVExcel("E:\Test.csv", "E:\Test_converted.xls", "csvtoexcel") Public Function func_ConversionCSVExcel(strSrcFile, strDestFile, Conversion) on error resume next Set objExcel = CreateObject("Excel.application") set objExcelBook = objExcel.Workbooks.Open(strSrcFile) objExcel.application.visible=false objExcel.application.displayalerts=...