Cannot click on some items of a list

Test environment: appium 1.4.13, device ipad mini (ios9.1)
Here are steps:

  1. 10 items will be returned in a list after doing search. 5 items displays in the screen. And others are hidden but can be viewed by scroll.
  2. I want to click on random item of the list. But click doesn’t work for some items. So I tried to click on all of them. It works from up to down. Code like this.
    for(int i=0;i<10;i++) {
    WebElement e = driver.findElements(By.className(xxx).get(i);
    ScreenTouch.tap(driver, e);
    }
    But not working from down to up. Click doesn’t perform for the tenth, the first to the fourth for(int i=9;i>=0;i–) {
    WebElement e = driver.findElements(By.className(xxx).get(i);
    ScreenTouch.tap(driver, e);
    }
    Could anyone help on this? Thanks.

you can’t click to elements that are outside of screen. that is why click from up to down working for first 5 visible items and does not work for first 5 NOT visible items from down to up.

suggested logic should include:
1 - wait page loaded and needed list is available
2 - get number of items in list: driver.findElements(By.className(xxx)).size()
3 - get Random from 0 to (size-1)
4 - check now that driver.findElements(By.className(xxx)).get(random).getLocation().getY() or driver.findElements(By.className(xxx)).get(random).getLocation().getX() is on screen
5 - step4 = false -> do swipe up/down/left/right - whatever needed
6 - repeat steps 4+5 until element is on screen
7 - do tap now

Thanks for your help. Actually, I tried with this method. X or Y is always 0 for some elements even after doing swipe up/down. I attached my code and the output result. From the twelfth, even when the element is on the screen after scrolling, the X or Y is still 0. So it doesn’t work using x or y. Do you have other idea? Many thanks.

List elementL = driver.findElements(By xxx);
size = elementL.size();
System.out.println(“size====” + size);
for(i=0;i< size;i++) {
element = elementL.get(i);
x = element.getLocation().getX();
y = element.getLocation().getY();
System.out.println(“i===" + i + “===x====" + x + “===y===”+y);
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width *3 / 4 , height / 3, width *3/ 4, height / 4, 1000);
}

Here is output in console.
size====25
i===0===x===325===y===116
i===1===x===325===y===125
i===2===x===325===y===134
i===3===x===325===y===143
i===4===x===325===y===152
i===5===x===325===y===161
i===6===x===325===y===170
i===7===x===325===y===179
i===8===x===325===y===188
i===9===x===325===y===197
i===10===x===325===y===206
i===11===x===0===y===0
i===12===x===0===y===0
i===13===x===0===y===0
i===14===x===0===y===0
i===15===x===0===y===0
i===16===x===0===y===0
i===17===x===325===y===599
i===18===x===0===y===0
i===19===x===0===y===0
i===20===x===0===y===0
i===21===x===0===y===0
i===22===x===0===y===0
i===23===x===0===y===0
i===24===x===0===y===0

rewrite your code NOT to use this:

       for (IOSElement el : item) {
            el......
        }

but use THIS:

IOSElement el;
for (int i=0; i<size; i++) {
  el = driver.findElements(By xxx).get(i)
  el ..... now print your x and y
}

I modified my code based on your suggestion. In my app, the button is located in the table cell. I want to click on the button. But I met java.lang.IndexOutOfBoundsException. I think that’s because the count of the buttons is changed randomly after swiping. It should be always 25, just like tabe cell count. Maybe this is appium bug. Could you check my code and output again? Is there other solution? Thanks a lot.

Code:
int size = driver.findElements(By.className(“UIATableCell”)).size();
System.out.println(“size====” + size);
for (int i=0; i< size; i++)
{
WebElement tableCellE = (WebElement) driver.findElements(By.className(“UIATableCell”)).get(i);
WebElement buttonE = (WebElement) driver.findElements(By.name(“invite”)).get(i);
tableCellCount = driver.findElements(By.className(“UIATableCell”)).size();
buttonCount = driver.findElements(By.name(“invite”)).size();
System.out.println(“tableCellCount===”+tableCellCount+"===buttonCount==="+buttonCount);
int x = buttonE.getLocation().getX();
int y = buttonE.getLocation().getY();
System.out.println(“i===”+i+"===x===" + x + “===y===”+y);
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width * 3 / 4 , height / 3, width* 3 / 4, height / 4, pf.during);
}

Output:
size====25
tableCellCount===25===buttonCount===25
i===0===x===325===y===116
tableCellCount===25===buttonCount===13
i===1===x===325===y===125
tableCellCount===25===buttonCount===17
i===2===x===325===y===134
tableCellCount===25===buttonCount===21
i===3===x===325===y===143
tableCellCount===25===buttonCount===25
i===4===x===325===y===152
tableCellCount===25===buttonCount===17
i===5===x===325===y===216
tableCellCount===25===buttonCount===21
i===6===x===325===y===225
tableCellCount===25===buttonCount===13
i===7===x===325===y===454
tableCellCount===25===buttonCount===25
i===8===x===325===y===243
tableCellCount===25===buttonCount===15
i===9===x===325===y===472
tableCellCount===25===buttonCount===20
i===10===x===325===y===371
tableCellCount===25===buttonCount===24
i===11===x===325===y===380
tableCellCount===25===buttonCount===25
i===12===x===325===y===499
tableCellCount===25===buttonCount===18
i===13===x===325===y===563

pls write your code more correctly. i do not understand it e.g. “for (int i=0; i{”

for (int i=0; i< size; i++). I’ve corrected it. Sorry for the misunderstanding

you need to investigate yourself where outofindex happens. either it is tableCellE,buttonE or … . and correct code. without original instead of “xxx” and pagesource i can’t help.

I know that indexOutOfBoundsException happens when the count of button is less than the index number. e.g. when the loop number is 13. The count maybe 15. Just like the log I pasted on the page, the count of the button changes randomly in each loop. So I think it’s appium bug. BTW, I have modified the code more clearly.