Problems using scroll_to and scroll_to_exact (Ruby and Android)

I’m trying to scroll through a list that contains unseen objects until the one I want is in view. I tried using scroll_to, but it was failing. When I looked at the source, I see this:

def scroll_to text
      text = %Q("#{text}")

      args = scroll_uiselector("new UiSelector().textContains(#{text})") +
             scroll_uiselector("new UiSelector().descriptionContains(#{text})")

      find_element :uiautomator, args
end

This explains my problem – only the text field is set to this value. The description field is empty. Why does Appium::Android::scroll_to and scroll_to_exact match both the text and description? This seem unnecessarily restrictive.

1 Like

I don’t think you understand the code. scroll_to matches on either the text or the description. If the text or description match then it’s considered a success.

This is in the docs

It looks like this is a bug in the appium server. At one point this worked correctly. I’ve filed an issue.

I should have dug deeper. I ended up writing my own code that worked around with the scrollable primitive, but I’m glad to hear I won’t have to maintain that.

Hi @willosser, I have the same problem with scrolling to the element. It performs the scroll, but can’t find an element. Can you share your solution with me please?

Hi @sdubov,

I liberated this code from the appium source. In our case, we don’t search by description, so

def scroll_to text
# This is a workaround for Appium issue 4311,
# It can and should be replaced by the following when the issue is resolved
# appium.driver.scroll_to text
text = %Q("#{text}")
args = appium.scroll_uiselector(“new UiSelector().text(#{text})”)
appium.driver.find_element :uiautomator, args
end

Thank you @willosser so much. It helped me a lot.

@sdubov @willosser This solution is not working for me, I made

def scroll_to(text)
text = %("#{text}")
args = scroll_uiselector(“new UiSelector().textContains(#{text})”)
find_element :uiautomator, arg
end
I am calling from my main script as

element = scroll_to(“Modified email recipents”)
element.txt

I get error as No element found

C:/Ruby22/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/response.rb:70:in `assert_ok’: No element found (Selenium::WebDriver::Error::NoSuchElement
Error)

@Abhilash_Iyer, what happens during execution? Does it scroll successfully or fail immediately?

This method scroll Only upto 15 index elment after tat it is throwing NoSuchElementFoundException in Android .Pls Help me with this issue @sdubov @willosser

public static void findScrollable() {

 MobileElement radioGroup = (MobileElement) driver
            .findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"
            + ".resourceId(\"com.product.excelacom.centuryprojectmanagement:id/linearLayout\")).scrollIntoView("
            + "new UiSelector().text(\"7313080\"));");
 
 radioGroup.click();

}

@suryasurendar, I haven’t had the chance to update our code base to start using by AndroidUIAutomator. Based on my experience using uiautomator before we adopted Appium, that sounds like a limitation of uiautomator, not Android. Unless you can find an example online, you’ll likely have to create your own scroll_to method to find the desired resource-id.

1 Like