Select an image from Android gallery using xpath

I would like to select the highlighted in red image using xpath.

I am using the following.

driver.findElement(By.xpath("//android.view.View[@clickable=‘True’]")).click();

the above code always selects the first image.
Can you please help me in selecting the fourth image

Can you try the following

driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.view.view[1][@clickable='True']")).click();

I can think of 2 ways

// get all images and then select index
List [WebElement] pic= driver.findElements(By.className("//android.view.View"));
pic.get(1).click(); // click based on index of image

// use xpath indexing something like below
driver.findElement(By.xpath("(//android.view.View[@clickable=‘True’])[1]")).click();
driver.findElement(By.xpath("(//android.view.View[@clickable=‘True’])[2]")).click();
driver.findElement(By.xpath("(//android.view.View[@clickable=‘True’])[3]")).click();
driver.findElement(By.xpath("(//android.view.View[@clickable=‘True’])[4]")).click();

These are approaches may or may not work with u r set up.

Hi Sruthi,

Thank you for the help, I tried with your code it failed to run.

Below is the error

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)

Session ID: a899de06-f969-45c5-9af5-d2e6996dc98e
*** Element info: {Using=xpath, value=//android.widget.LinearLayout[1]/android.view.view[1][@clickable=‘True’]}

List img= driver.findElementsByName(“Photo (13 October 2015)”);
img.get(0).click ( );

Try with this code…hopefully it will work. If this code not worked then one more solution to install any third party gallery app from play store which will provide proper locators…

Or send me uiautomatorviewer dump. So we can also analysis locators.
Save image from uiautomatorviewer
It will generate two files. One snapshot and one dump file upload both.

Thanks
Srikanth

dump_3756825598817410535.txt (12.7 KB)

Hi Srikanth,

As i am unable to upload the uix file i have renamed the file to txt.

Please see both attachments.

Try with this code, Hopefully it will work
List img = driver.findElements(By.name(“Photo (13 October 2015)”));
img.get(0).click();

Automatically WebElement is delete by website …Refer the below snapshot for …correct code

If u want to click anything based on text present in ‘content desc’ below can be used. But it is more of hardcoding kind of thing. So using class name and xpath indexing is recommended because if image name is changed it will fail.

driver.findElement(By.AccessibilityID(“Photo (13 October 2015)”)).click().
driver.findElement(By.name(“Photo (13 October 2015)”)).click();

This is how it worked. Thank you all for the support

driver.findElement(By.xpath("//android.widget.ImageView[@content-desc=‘Photo taken on 27 Oct 2015 14:46:17’]")).click();

when i put the same code
driver.findElement(By.xpath("//android.widget.ImageView[@content-desc=‘Photo taken on 13 May 2016 12.50’]")).click();

I am getting
Element not found exception…
please help to reslove this error
I am working with nexus 5

Hello,
some thing i want to do but i facing the issue while selecting the particular frame from gallery with the help of Ui viewer , using android device (lenovo k5).please help