Ruby: Scroll to element and click

Is there any way that I can scroll to an element and click in android using appium with ruby?

I have tried a variety of things:

scroll_to(text, scrollable index)
find_element(text: "India").click

scroll_to_exact("India")
find_element(text: "India").click

def scroll_to text

appium.driver.scroll_to text

text = %Q("#{text}")
args = appium.scroll_uiselector(“new UiSelector().text(#{text})”)
appium.driver.find_element :uiautomator, args
end

def scroll_to_exact(text, scrollable_index = 0) end
Basically, I have a native app, consider whatsapp sign up, where you click on the country code tab and scroll and select a country. I would want to scroll and select “India” text in this.

I tried even using cordinates:

execute_script ‘UIATarget.localTarget().flickFromTo({x:144, y:173},{x:677, y:73}).click’
All these would just scroll to the element, and again scroll back to the top of the page. Is there anyway, i can ask it to stop once identified and click on it

All these would just scroll to the element, and again scroll back to the top of the page.

This behaviour, using UiScrollable#scrollIntoView depends on uiautomator framework. Sometimes, I also see such behaviour.
You can implement scroll action with TouchAction and find_element and check the element alternatively such as:

(0...times).each do            # continue to scroll the element `times` time
  Appium::TouchAction.new.swipe(xxxx).perform # scroll down
  ele = check_element(element) # check if the element is displayed or not
  return ele unless ele.nil?   # return the element if the element is displayed
end
1 Like

@KazuCocoa
I get an error that says “undefined method `fetch’”

Can you please tell me what is ‘xxxx’ in the second line of code? Do I need to pass the id or the text of the element there? or the cordinates?

@KazuCocoaI tried implementing it, but
It scrolls so fast like its playing a poker or gamble, spinning the wheel, and the list goes no where.

Then(/^I scroll to country India$/) do
  #find_element(name: "Cayman Islands").click
  Appium::TouchAction.new.swipe(start_x: 144, start_y: 1634, end_x: 144, end_y: 149).perform
  Appium::TouchAction.new.swipe(start_x: 144, start_y: 149, end_x: 144, end_y: 1634).perform
  find_element(name: "Croatia").click

If you’d like to control scroll speed, you can use : duration parameter like https://github.com/appium/ruby_lib/blob/80f80715dcee6de968700ece40894222af41579a/lib/appium_lib/device/touch_actions.rb#L151 .

@reach2jeyan,

Why are you scrolling both directions?

Appium::TouchAction.new.swipe(start_x: 144, start_y: 1634, end_x: 144, end_y: 149).perform
Appium::TouchAction.new.swipe(start_x: 144, start_y: 149, end_x: 144, end_y: 1634).perform

First you swipe from bottom to top, then top to bottom. Also, this scrolls only once. Look at KazuCocoa’s original code - scroll down, return the element if it’s visible, else repeat.