Appium not able to perform click() operation in list of webElement

Hello,

I am trying to run one @Test method which contains click() operation on list of WebElement driver.findElements(By.id(“news_item_title”)).get(0).click(); in AWS Device farm. After running scripts @Test method is failing. org.openqa.selenium.remote.SessionNotFoundException:. This exception is coming and after downloading logs i got to know that Element[] is null: (0) coming continuously, So Appium does not able to click on the items and after 60 seconds session gets expire. And test case failed. Below is the log.

2016-08-17 12:12:47:971 - info: <-- POST /wd/hub/session/b79747a5-84f5-4870-acb4-9e744804fe6f/touch/perform 200 1627.142 ms - 76 {“status”:0,“value”:true,“sessionId”:“b79747a5-84f5-4870-acb4-9e744804fe6f”}
2016-08-17 12:12:47:985 - info: --> POST /wd/hub/session/b79747a5-84f5-4870-acb4-9e744804fe6f/element {“using”:“id”,“value”:“news_item_title”}
2016-08-17 12:12:47:985 - info: [debug] Waiting up to 0ms for condition
2016-08-17 12:12:47:986 - info: [debug] Pushing command to appium work queue: [“find”,{“strategy”:“id”,“selector”:“news_item_title”,“context”:"",“multiple”:false}]
2016-08-17 12:12:48:000 - info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“id”,“selector”:“news_item_title”,“context”:"",“multiple”:false}}
2016-08-17 12:12:48:001 - info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
2016-08-17 12:12:48:001 - info: [debug] [BOOTSTRAP] [debug] Got command action: find
2016-08-17 12:12:48:001 - info: [debug] [BOOTSTRAP] [debug] Finding news_item_title using ID with the contextId: multiple: false
2016-08-17 12:12:48:001 - info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.july.ndtv:id/news_item_title]
2016-08-17 12:12:58:051 - info: [debug] Responding to client with success: {“status”:0,“value”:{“ELEMENT”:“2”},“sessionId”:“b79747a5-84f5-4870-acb4-9e744804fe6f”}
2016-08-17 12:12:58:056 - info: <-- POST /wd/hub/session/b79747a5-84f5-4870-acb4-9e744804fe6f/element 200 10070.851 ms - 87 {“status”:0,“value”:{“ELEMENT”:“2”},“sessionId”:“b79747a5-84f5-4870-acb4-9e744804fe6f”}
2016-08-17 12:12:58:056 - info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,“value”:{“ELEMENT”:“2”}}
2016-08-17 12:12:58:090 - info: --> GET /wd/hub/session/b79747a5-84f5-4870-acb4-9e744804fe6f/element/2/displayed {}
2016-08-17 12:12:58:091 - info: [debug] Pushing command to appium work queue: [“element:getAttribute”,{“elementId”:“2”,“attribute”:“displayed”}]
2016-08-17 12:12:58:109 - info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“element:getAttribute”,“params”:{“elementId”:“2”,“attribute”:“displayed”}}
2016-08-17 12:12:58:109 - info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
2016-08-17 12:12:58:109 - info: [debug] [BOOTSTRAP] [debug] Got command action: getAttribute
2016-08-17 12:13:08:211 - info: [debug] Responding to client with success: {“status”:0,“value”:true,“sessionId”:“b79747a5-84f5-4870-acb4-9e744804fe6f”}
2016-08-17 12:13:08:212 - info: <-- GET /wd/hub/session/b79747a5-84f5-4870-acb4-9e744804fe6f/element/2/displayed 200 10122.240 ms - 76 {“status”:0,“value”:true,“sessionId”:“b79747a5-84f5-4870-acb4-9e744804fe6f”}
2016-08-17 12:13:08:217 - info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,“value”:“true”}
2016-08-17 12:13:08:218 - info: --> GET /wd/hub/session/b79747a5-84f5-4870-acb4-9e744804fe6f/element/2/enabled {}
2016-08-17 12:13:08:219 - info: [debug] Pushing command to appium work queue: [“element:getAttribute”,{“elementId”:“2”,“attribute”:“enabled”}]
2016-08-17 12:13:08:231 - info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“element:getAttribute”,“params”:{“elementId”:“2”,“attribute”:“enabled”}}
2016-08-17 12:13:08:231 - info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
2016-08-17 12:13:08:232 - info: [debug] [BOOTSTRAP] [debug] Got command action: getAttribute
2016-08-17 12:13:18:259 - info: [debug] Responding to client with success: {“status”:0,“value”:true,“sessionId”:“b79747a5-84f5-4870-acb4-9e744804fe6f”}
2016-08-17 12:13:18:260 - info: <-- GET /wd/hub/session/b79747a5-84f5-4870-acb4-9e744804fe6f/element/2/enabled 200 10041.479 ms - 76 {“status”:0,“value”:true,“sessionId”:“b79747a5-84f5-4870-acb4-9e744804fe6f”}
2016-08-17 12:13:18:260 - info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,“value”:“true”}
2016-08-17 12:13:18:265 - info: --> POST /wd/hub/session/b79747a5-84f5-4870-acb4-9e744804fe6f/elements {“using”:“id”,“value”:“news_item_title”}
2016-08-17 12:13:18:266 - info: [debug] Waiting up to 0ms for condition
2016-08-17 12:13:18:266 - info: [debug] Pushing command to appium work queue: [“find”,{“strategy”:“id”,“selector”:“news_item_title”,“context”:"",“multiple”:true}]
2016-08-17 12:13:18:279 - info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“id”,“selector”:“news_item_title”,“context”:"",“multiple”:true}}
2016-08-17 12:13:18:279 - info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
2016-08-17 12:13:18:280 - info: [debug] [BOOTSTRAP] [debug] Got command action: find
2016-08-17 12:13:18:280 - info: [debug] [BOOTSTRAP] [debug] Finding news_item_title using ID with the contextId: multiple: true
2016-08-17 12:13:18:281 - info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[RESOURCE_ID=com.july.ndtv:id/news_item_title]
2016-08-17 12:13:18:281 - info: [debug] [BOOTSTRAP] [debug] getElements selector:UiSelector[RESOURCE_ID=com.july.ndtv:id/news_item_title]
2016-08-17 12:13:18:282 - info: [debug] [BOOTSTRAP] [debug] Element[] is null: (0)
2016-08-17 12:13:18:282 - info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=0, RESOURCE_ID=com.july.ndtv:id/news_item_title]
2016-08-17 12:13:28:339 - info: [debug] [BOOTSTRAP] [debug] Element[] is null: (1)
2016-08-17 12:13:28:339 - info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=1, RESOURCE_ID=com.july.ndtv:id/news_item_title]
2016-08-17 12:13:38:439 - info: [debug] [BOOTSTRAP] [debug] Element[] is null: (2)
2016-08-17 12:13:38:439 - info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=2, RESOURCE_ID=com.july.ndtv:id/news_item_title]
2016-08-17 12:13:48:493 - info: [debug] [BOOTSTRAP] [debug] Element[] is null: (3)
2016-08-17 12:13:48:495 - info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=3, RESOURCE_ID=com.july.ndtv:id/news_item_title]
2016-08-17 12:13:58:582 - info: [debug] [BOOTSTRAP] [debug] Element[] is null: (4)
2016-08-17 12:13:58:582 - info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=4, RESOURCE_ID=com.july.ndtv:id/news_item_title]
2016-08-17 12:14:08:738 - info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[RESOURCE_ID=android:id/news_item_title]
2016-08-17 12:14:08:738 - info: [debug] [BOOTSTRAP] [debug] getElements selector:UiSelector[RESOURCE_ID=android:id/news_item_title]
2016-08-17 12:14:08:770 - info: [debug] [BOOTSTRAP] [debug] Element[] is null: (0)
2016-08-17 12:14:08:798 - info: [debug] [BOOTSTRAP] [debug] getElements tmp selector:UiSelector[INSTANCE=0, RESOURCE_ID=android:id/news_item_title]
2016-08-17 12:14:18:267 - info: [debug] Didn’t get a new command in 60 secs, shutting down…
2016-08-17 12:14:18:267 - info: Shutting down appium session

Regards,
Sumit

I would suggest to store the elements in the list and then iterate through each element.

List elementList = driver.findElements(By.id(“news_item_title”));
System.out.println(“Total elements :” + elementList.size());
elementList.get(0).click();

Thanks for the reply @Suman. I also did the same but its not working. :frowning:

Can you please post screen-shot of appium inspector or uiautomatorviewer, which ever you are using to find elements. Sorry, see the correct code below

List<WebElement> elementList = driver.findElements(By.id("news_item_title"));
System.out.println("Total elements :" + elementList.size());
elementList.get(0).click();

Sure Suman.

Use below code. I am using xpath locator strategy instead of id. Is your app hybrid? Does it switches between native and webview?

> List<WebElement> newsArticles = (List<WebElement>) driver.findElements(By.xpath("//android.widget.TextView[contains(@resource-id,'news_item_title')]"));
> 		newsArticles.size();

Yes my app is hybrid. But this page is complete in native view. And also can you please let me know if any specific reason why we use Xpath when we have id as locator?

ID should work. I am not sure why it’s not working though. Is this app home page? Do you wait for app to load home page before you find article list?

Yes i am waiting till elements to be visible. Also i observed some times elements locating and immediately and clicked on news. But most of the time its taking more than 90 seconds. And in device farm session is only for 60 sec. If appium does not perform any action within 60 sec appium server is shutting down.

Increase the command time out in capabilities. It will keep the server running and you won’t get time out error message.

capabilities.setCapability(“newCommandTimeout”, 120);

How are you adding wait?

WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.presenceOfElementLocated(BY-LOOCATOR-STRATEGY-HERE));

I am running my scripts on device farm. Their we cant set any DesiredCapabilities only we have to upload the .apk file and the zipped file of scripts. All configurations done by AWS device farm itself. only we need to set

final String URL_STRING = “http://127.0.0.1:4723/wd/hub”;
URL url = new URL(URL_STRING);

How are you adding wait?–> Yes i am using explicit wait.

Did the above solution for xpath work?
If you know which feed url is being used by your app to display news articles then you can also read articles from json to ensure you are testing live data.

No Suman thats also didn’t worked :(.

Yes i know feed url which dev team are using in my app. Can you please tell me how to get data from feed and verify is it displaying in our app?

You can use the below logic to read json from feel url

        URL website = new URL(FEED-URL-HERE);
	ReadableByteChannel rbc = Channels.newChannel(website.openStream());
	FileOutputStream fos = new FileOutputStream(("feed.json"));
	fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
	String s = readFile("feed.json");
        String fullTitle = new JSONObject(s).getJSONArray("items").getJSONObject(artNum).getJSONObject("headline").getString("full");

In the above example I am storing feed in json file and then using jsonObject I am reading the values. I am checking for “headline” but this could vary depending on your json objects. Open your json file to get the better picture. Can you share your app debug apk? I can check the title click for you.

Thanks Suman, will check and let you know if it works for me
. :slight_smile: