Can't find element in android native app launched from another native app

Platform: Android
Android Version: 4.1.1
Appium Version: v1.4.16
Run on: Real Device
Language Used: java selendroid
OS: Linux-Ubuntu

Issue: Cannot able to find elements in one of the Native Application (‘say app2’) which was launched from another Native Application (‘say app1’).

Steps Followed:

  1. Using desired capabilities launched Activity1 of Application1: “com.pkg.ui.app1.activity1”,
  2. Navigated to a GridView to click on an Image View button to open Application2,
  3. Tried to find an element with id “btn1” in the Application2.

Code1 Tried:

try {
  driver.findElement(By.id("btn1"));
} catch (NoSuchElementException e) {
  System.out.println("->-> Not Found!!");
}

Code1 Log:

info: --> POST /wd/hub/session/f6b9df17-2448-6228-d782-eb5973508596/element {"using":"id","value":"btn1"}
info: [debug] Proxying command to localhost:8080
info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/f6b9df17-2448-6228-d782-eb5973508596/element","method":"POST","json":{"using":"id","value":"btn1"}}
info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"Element was not found."},"sessionId":"f6b9df17-2448-6228-d782-eb5973508596"}
info: <-- POST /wd/hub/session/f6b9df17-2448-6228-d782-eb5973508596/element 500 50139.518 ms - 201 

Code2 Tried:

try {
  driver.findElement(By.xpath("//ImageView[@id='btn1']"));
} catch (NoSuchElementException e) {
  System.out.println("->-> Not Found!!");
}

Code2 Log:

info: --> POST /wd/hub/session/57151ec0-235d-e164-feba-d6855c94bf9a/element {"using":"xpath","value":"//ImageView[@id='btn1']"}
info: [debug] Proxying command to localhost:8080
info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/57151ec0-235d-e164-feba-d6855c94bf9a/element","method":"POST","json":{"using":"xpath","value":"//ImageView[@id='btn1']"}}
info: [debug] Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command.","origValue":"CATCH_ALL: io.selendroid.server.common.exceptions.SelendroidException: No open windows.\n\tat io.selendroid.server.model.internal.AbstractNativeElementContext.getElementTree(AbstractNativeElementContext.java:103)\n\tat io.selendroid.server.model.internal.AbstractNativeElementContext.findElementsByXPath(AbstractNativeElementContext.java:211)\n\tat io.selendroid.server.model.internal.AbstractNativeElementContext.findElementByXPath(AbstractNativeElementContext.java:201)\n\tat io.selendroid.server.model.internal.AbstractNativeElementContext.findElement(AbstractNativeElementContext.java:193)\n\tat io.selendroid.server.model.By$ByXPath.findElement(By.java:231)\n\tat io.selendroid.server.model.DefaultSelendroidDriver.findElement(DefaultSelendroidDriver.java:97)\n\tat io.selendroid.server.handler.FindElement.safeHandle(FindElement.java:42)\n\tat io.selendroid.server.handler.SafeRequestHandler.handle(SafeRequestHandler.java:86)\n\tat io.selendroid.server.AndroidServlet.handleRequest(AndroidServlet.java:264)\n\tat io.selendroid.server.common.BaseServlet.handleHttpRequest(BaseServlet.java:67)\n\tat io.selendroid.server.common.http.ServerHandler.channelRead(ServerHandler.java:53)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)\n\tat io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)\n\tat io.netty.handler.traffic.AbstractTrafficShapingHandler.channelRead(AbstractTrafficShapingHandler.java:223)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)\n\tat io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)\n\tat io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)\n\tat io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)\n\tat io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:163)\n\tat io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:148)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)\n\tat io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)\n\tat io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787)\n\tat io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:125)\n\tat io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)\n\tat io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLoop.java:430)\n\tat io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:384)\n\tat io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)\n\tat io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)\n\tat io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)\n\tat java.lang.Thread.run(Thread.java:856)\n"},"sessionId":"57151ec0-235d-e164-feba-d6855c94bf9a"}
info: <-- POST /wd/hub/session/57151ec0-235d-e164-feba-d6855c94bf9a/element 500 79.744 ms - 3417

Info:

  1. Can able to access any element in the app1 without any issues, after navigating to app2 can’t able to access any element on this application.
  2. The button under discussion is inside a fragment.
  3. The id is taken from its fragment page layout xml file.

Please help me in resolve this…
Thanks in advance…

@LohitRaj,

If I had this problem, I would dump page source and see what’s visible
appium_driver.get_source

You might find it more readable if you convert it with nokogiri:
page_source = Nokogiri::XML(appium_driver.get_source)

Check that the resource id exists in the view.

@willosser,

Thanks for your reply.

I suppose you are referring to a ruby method ‘appium_driver.get_source’ , but here I’m using java in my code.

Does this method work for a native application, if yes can you please point me to a java equivalent code.

Anyways, I have used both UIAutomatorViewer and searched in the xml layout of the activity page in the source code to verify the presence of the button, and seems it is available in the layout xml as well as can be viewable in UIAutomatorViewer (from where I have picked up the ID of the button to find the element).

Any other leads are more welcome…