Getting Child Element from Specific Parent element

Hello ,

I want to validate a scenario in which , I have to check the price of product listed in RecycleView .

Problem is There is Multiple Linear Layout with same id .

I tried follwoing code , I am able to get Text of Product , nut not able to get price of that product .

WebElement product_name=driver.findElement(By.xpath("//android.widget.TextView[@resource-id='com.snapdeal.main:id/productTitle' and @text='Healthvit Selenium 200mcg 60 Capsules']"));

I do I get it working. A working code is much more helpful.


example of finding parent item in iOS (in Android should be similar)

elem = ((IOSDriver) driver).findElement(MobileBy.xpath("//*[@value = " + descr + "]/.."));

parent of parent:

elem = ((IOSDriver) driver).findElement(MobileBy.xpath("//*[@value = " + descr + "]/../.."));

Thanks for that , I know this , but I am not able to figure out how to target that , NO such elemetn found exception.
I tried following path,

WebElement product_name=driver.findElement(By.xpath("//android.widget.LinearLayout[@resource-id='com.snapdeal.main:id/product_text_container']/android.widget.RelativeLayout/android.widget.TextView[@text='Healthvit Selenium 200mcg 60 Capsules']"));
        System.out.println("Price of Product :"+product_name.findElement(By.xpath("//android.widget.LinearLayout[@resource-id='com.snapdeal.main:id/rating_layout']"
        		+ "/android.widget.RatingBar/android.widget.LinearLayout/android.view.ViewGroup/android.widget.TextView[@resource-id='com.snapdeal.main:id/productDisplayPrice']")).getAttribute("text"));

try mine. first find parent of parent (this give you LinearLayout) by “text” attribute with needed text e.g. “Healthvit Selenium 200mcg 60 Capsules”.
now you can find whatever you like e.g. get all TextViews inside and check all you need.

Can please rewrite xpath based on screenshots provided.
I am not able to relate it. tried all. And id of Product Name amd Product Price has different ids.

can you share pageSource? driver.getPageSource().

codebeautify.txt (34.6 KB)

String descr = “Healthvit Selenium 200mcg 60 Capsules”;
parent = ((AndroidDriver) driver).findElement(MobileBy.xpath(“//*[@text = '” + descr + “']/…/…”));

not sure (not worked before in such way but try):


this should work:

1 Like

Thanks it work .
Was not aware of //* and /… in xpath.