Scroll to element in Android

Hello,
I’m looking for working way , effective way to scroll to element that it’s not appear in the screen.
I tried all the ways here, and nothing worked
Attached picture.

Thanks

How I do this:

  1. check for element in begin/rescue block (Ruby try/catch)
  2. if not found scroll one screen
  3. check for element in begin/rescue block (Ruby try/catch)
  4. if not found scroll one screen
  5. and so on. I will do this 3-5 times before I give up, but that’s as long as any scrollable screen in the app I test goes

My code for scrolling one screen down calculates the dimensions of the screen, so it’s portable to different sized devices:

# scroll_one_screen_down
#
# Uses the dimensions of the screen to calculate scrolling exactly one screen
def scroll_one_screen_down
  window = apm.driver.window_rect
  action_builder = apm.driver.action
  input = action_builder.pointer_inputs[0]
  action_builder
    .move_to_location(window.width / 2, window.height * 8 / 10)
    .pointer_down(:left)
    .pause(input, 1)
    .move_to_location(window.width / 2, window.height / 5)
    .pause(input, 1)
    .release
    .perform
end

@I7210I try this:

AndroidElement PermissionElement = driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"Permissions\"))");
PermissionElement.click();
1 Like

@wreed
Thank you, there is something close in JAVA?
@ZaidanJawaid I saw that, also added that to my code and I found it very useful since it’s scanning the page (up and down) for the text, what I eventually didn’t found is the way to send to that function (let’s say we will put it into function) is how to send to that method the text I want to search , I mean to that part .text(\"Permissions\"))");

also what is the best option to choose from to be along side with the w3c ?

Thank you

examples - https://appium.github.io/appium.io/docs/en/writing-running-appium/tutorial/swipe-tutorial/

Thanks, they are going with the W3C?
And how to put the text into function?

UiScrollable and UiSelector are native Android commands. TouchAction needs update to w3c.

Thanks, there is where I can see more on that commands and the usage?
And also how to enter the text into it from outside the command?
I mean to the textFromOutside part, I tried to devide the Apostrophes to two parts but I didn’t succed with that

AndroidElement PermissionElement = driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text('+textFromOutside+'))");
PermissionElement.click();

https://developer.android.com/reference/androidx/test/uiautomator/UiScrollable

https://developer.android.com/reference/androidx/test/uiautomator/UiSelector

Just scroll to input and enter as usual.

That’s the thing that what I tried didn’t work:

public void clickAndScroll(String textFromOutside){
AndroidElement PermissionElement = driver.findElementByAndroidUIAutomator("new 
UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new 
UiSelector().text('+textFromOutside+'))").click();

That is not working with the text that I send to the function

It depends on screen. Sometimes it need a bit help. For example add scrollView id when we have more then two on screen.
Also you do not say what happen. Nothing or it scrolling to end and missing needed element…

Just need to find it and click on it, but I can’t send to the method the text…you know how?

So step one scroll to it. Step second click.
You can swipe with many methods mentioned in tutorial link above. When some Android specific methods fail swipe screen working 100%. Just swipe - check needed el and repeat if not found.

I understand the two options,
But interest to know if there is option to send to that method mention above , text from outside like I tried and not to hard code the text

@I7210I What I understand is you want something like this;

public void clickAndScroll(String textFromOutside){
AndroidElement element = driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+textFromOutside+"\"))");
element.click(); 
}

@ZaidanJawaid Thank you, exactly that, but the thing is how to sen the text from outside the function to inside it , what you wrote is not working in my IDE in runtime if I send text from outside the function

@I7210I It should work, make sure its working if you define the String inside the function and also try with some other text present on the page.

@ZaidanJawaid, sostrange, according to what I did it’s work only if I put the text as you put it :

    AndroidElement PermissionElement = driver.findElementByAndroidUIAutomator("new 
    UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new 
    UiSelector().text(\"Permissions\"))");

but from outside the function or something that it’s not hardcoded inside, doesn’t work , so strange,

Cause look at examples I gave in tutorial. You code it wrong (c) Someone clever

Or look ar @ZaidanJawaid example. See the difference in:

// correct
AndroidUIAutomator("new UiSelector().textContains(\"" + name + "\")")
// your code
[quote="I7210I, post:10, topic:35529"]
scrollIntoView(new UiSelector().text('+textFromOutside+'))")
[/quote]
// here are several errors.
1 Like

Thank you,
Found the problem now