Help with Swiping/Scrolling to an element

I needed help trying to swipe to an element that is not in my current window. For example if I had a list of table cells that represent videos the first and second video can be seen on my phone screen and I can click on them but if I wanted to play the tenth video, how would I swipe far enough so that I would be able to click on the element?

I have used the swipe method with hard coded values but its kind of a blind approach. My issue is that I can not find a flexible solution for getting to a specific element.

Any ideas or information, on how the scrollTo and swipe methods work would be helpful.

I find the scrollTo method because I am not sure what the parameter should be it says string text. For IOS does this reference value, or name?

With the swipe method I have tried putting element1.getY() for the initial y value and element2.getY() for the second y value but this does not seem to work out for me.

Thanks ttys

1 Like

Cant you just swipe and look for element until you find it, like

loop until element found
    look for element

Hey there,

I had a similar issue earlier.
Essentially, the TouchAction commands weren’t responsive when trying to swipe within a table view. Reading through some of the posts this had been an issue with iOS 7. I was still having problems with this in iOS 8.
I worked around this by applying the following logic.
Note: This is still prototype like code. It needs to be improved:

  1. iOS:
    Strategy: Use find_element by uiautomation.
    The upside is I can tap on any tableCell in the tableView, even the ones off screen.
    The downside is that I can only tap by row number, not by anything else at the moment.

This post is a pretty good overview on finding elements by uiautomation: How to use/findElements - ByIosUIAutomation

Once I figured out the view heirarchy, I applied logic like this:

  def clickThisContentItem(my_content_item,row)
        @courseOutlineListItem = {uiautomation: ".scrollViews()[1].tableViews()[0].cells()[#{row}]"}
        @courseOutlineListItemTextField = {uiautomation: ".scrollViews()[1].tableViews()[0].cells()[#{row}].staticTexts()[0]"}
        @courseOutlineListItemText = wait {find_element(@courseOutlineListItemTextField).text}
        if (@courseOutlineListItemText != my_content_item)
          puts "\tWe expected: #{my_content_item}"
          puts "\tWe found in row #{row}:  #{@courseOutlineListItemText}"
          raise Exception, "The file we are clicking on is not what we expected!"
        wait {find_element(@courseOutlineListItem).click}
  1. Android
    Using arc, I was able to pinpoint which elements matched the tableCells.
    Then I applied a find_elements by id command.
    I was able to again click on elements offscreen.

Here’s the gist of this code here.

 def clickThisContentItem(my_content_item,row)
        set_wait timeout=40
        @courseOutlineListItems = wait {find_elements(:id, "")}
        @courseOutlineListItemTexts = { |element| element.text }
        iRow = iRow - 1  #android is "0" based in its element counts so we subtract 1
        @courseOutlineListItemText = @courseOutlineListItemTexts[iRow]

        if (@courseOutlineListItemText != my_content_item)
          puts "\tWe expected: #{my_content_item}"
          puts "\tWe found in row #{row}:  #{@courseOutlineListItemText}"
          raise Exception, "The file we are clickcking on is not what we expected!"


I’m not sure if you want to actually emulate the swipe in this. In that case, @Telmo_Cardoso makes a good suggestion.
I would use this particular “swipe/scroll strategy” then.

 execute_script 'mobile: scrollTo', :element => b.ref

Again, my code is still quite hacky and needs to be cleaned up. Hopefully it points you in the right direction. Sorry if it doesn’t


1 Like

I prefer to go with scrollTo() method as it is simple and readable.

If your 10th video is having some name like “Arnold’s terminator” and if you instantiate your driver with AndroidDriver, then you just need to use below methods:

driver.scrollTo(“Arnold’s terminator”)
WebElement ele=driver.findElement(“Use your preference to fine this element”)
//then click or do whatever you want like: etc…

Further simple way is:
driver.scrollTo(“Arnold’s terminator”).click();

1 Like

Ud makes some good points. I would use that strategy.