How can I find the relativeLayout whose child contains a text in scrollView


This is a sample hierarchy which is available for a scrollView set for “”.

How do I scroll and get “relativeLayout” element having the first textView as mentioned above ?

I tried the following code :

MobileBy.AndroidUiAutomation("new UiScrollable(new UiSelector().className("").scrollable(true)).scrollIntoView(new UiSelector().textContains(\"patternString\"))";));

– This code runs but gives me only the text enclosed element i.e “android.widget.TextView”. But I want is it’s parent “android.widget.RelativeLayout”.

I think I’ve to map my pattern with childSelector() and fromParent methods of UiSelector() which I’m not able to figure it out exactly.

Anyone help would be appreciated.

@Karthik_Sriharsha i stopped on:

  1. scroll to element same as you with UiScrollable
  2. now when we have element on screen find parent with:
WebElement parent = ((AndroidDriver) driver).findElement(MobileBy.xpath("//*[@text = '" + descr + "']/.."));

Thanks again Aleksei !! Now further to finding the parent, I did try to perform the following “swipe : left” option as follows :

// This swipe left does enable "rename / delete - buttons

WebElement sampleTextListElement = DriverUtils.getLocalAndroidDriver().findElement(MobileBy.AndroidUIAutomator(
				"new UiScrollable(new UiSelector().className(\"\").scrollable(true)).scrollIntoView("
				+ "new UiSelector().textContains(\"Aaaaa\"));"));
		WebElement parent = DriverUtils.getLocalAndroidDriver().findElement(MobileBy.xpath(""
				+ "//*[@resource-id ='tv_shopping_list_item_line1' and contains(@text,'Aaaaa')]/.."));

	    HashMap<String,String> scrollObj = new HashMap<String,String>();
	    scrollObj.put("direction", "left");
	    scrollObj.put("element", ((RemoteWebElement) parent).getId());
	    ((JavascriptExecutor) DriverUtils.getLocalAndroidDriver()).executeScript("mobile: swipe", scrollObj);

I see the following error ::

org.openqa.selenium.WebDriverException: Method has not yet been implemented (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10 milliseconds

Additionally I tried the following way using TouchAction :

        int startx = (int) (parent.getSize().getWidth()*0.85);
		int floaty = (int) (parent.getSize().getHeight()*0.5);
		int endx  = (int) (parent.getSize().getWidth()*0.15);
new TouchAction(DriverUtils.getLocalAndroidDriver()).press(parent,startx,floaty).waitAction(Duration.ofMillis(3000)).moveTo(parent,endx, floaty).release().perform();

But using TouchAction, it does a longPress and opens relevant set of action.

What would be ideal way to do a leftSwipe on parent element ?

  1. try with scrollable like:
"new UiScrollable(new UiSelector()).scrollIntoView(new UiSelector().textContains(\""+text+"\"));"
  1. with manual scroll longTap happens due to “waitAction(Duration.ofMillis(3000))” reduce 3000 -> 300

Will check and let you know :slight_smile: !! I’m always overwhelmed by your responses. Thanks a ton.