Get Element Count returns the list of elements present in the screen view for mobile device

Hi All,

I have a requirement, where I need to traverse through all the matching xpath & match the count with a defined number.

The requirement seems straight forwards but the problem I am facing is, Get Element Count or Get Matching Xpath Count returns the list of elements present in the screen view. i.e. there are more elements matching the Xpath, but for that i need to scroll down on my mobile device for a native app.

I would like to know, if there is a way i can get the element count without using the scroll action.

To tell you more about the behaviour, Appium inspector captures the screenshot for the screen and then Get Element Count or Get Matching Xpath Count returns the list of elements present only in the view of inspector. However, Get Element Count or Get Matching Xpath Count should not depend on the screen view rather it should match with all the elements matching the xpath similar to what is there in web browser for UI automation.

HI Sohit,

If your xpath gives you a list of matches then you can use the built-in library’s Get Length to give you the number of items in the list if that’s all you need?

If you need to count the items from the list that contains a specific value then Get Count or Collections Library’s Get Match Count might be usefull.

Dave.

1 Like

Hi Dave,

Thanks for the response. I will explain more on my requirement, here it goes:-

  1. On a mobile device screen, there are certain elements the count of which are 6.
  2. At any given time, mobile device shows only 4. So, when i use either of Get Element Count or Get Matching Xpath Count, i am getting the count as 4.
  3. Now, i have to click on all of 6 elements.
  4. As only 4 elements are seen at any given time, the only way i feel is, to swipe up to get to the hidden 2 elements & then perform click action.

I would like to know, if there is another way of doing this, without scroll/swipe?

Hi Sohit,

What happens when you do it manually? do only 4 items show on the screen, and you need to swipe to show the two hidden elements?
Is this something thats screen size dependant? i.e. if you tested with a larger device’s screen size would all 6 items show without scrolling?

I’m suspecting it’s your app is only loading 4 items (because that’s all thats initially visible) and this is why your xpath count is 4, and then the swipe action triggers an event to load the next page of 4 items (but you only have 6 so only 2 are returned)

Which library are you using? I don’t have much experience with mobile apps (Appium?) but if it’s just a web site being displayed on a mobile device I probably can help.

Dave.

Hi Dave,

That’s exactly the behaviour with my native mobile app. I am aware of traversing through list of items using xpath that matches visible & invisible items (i.e. the items that requires scroll).
However, Get Element Count returns only the list of visible items on the mobile app. That’s quite strange to me.

To answer your question, i am using AppiumLibrary and the app loads all the 6 items at once. But only 4 of them are visible on the screen & hence Get Element Count returns 4. But, if i scroll down to the end, i get to see the other 2 elements as well.

I don’t think so, scrolling everytime is the best option to get list of elements. Moreover, i have to check the duplicate enteries as well (if scroll is the only option left)

I have come across few stackoverflow threads & there seems to be a limitation. Do you know if there is a workaround to bypass this?

Hi Sohit,

As I said I have no experience with AppiumLibrary, all I can do is read the documentation and make suggestions…

I would say have a play with:

And also have a read about how to do this:

Appium additionally supports some of the Mobile JSON Wire Protocol locator strategies.

Failing that, hopefully someone who is familiar with AppiumLibrary can make some suggestions.

Dave.