'Find element using explicit wait within another element

I have the following code:

List<MobileElement> messages = driver.findElements (By.id ("body_bubble")); //find all messages
MobileElement lastMessage = messages.get (messages.size () - 1); //get last message

// xpath query to check if the message is delivered
String xpathDeliveryStatus = ".//*[contains(@resource-id, 'delivered_indicator') or contains(@resource-id, 'read_indicator') or contains(@resource-id, 'sent_indicator')]"; 
MobileElement deliveryStatus = lastMessage.findElement (By.xpath (xpathDeliveryStatus));

I want to do the same except that I want to find the deliveryStatus variable using explicit wait.
So I want to find the deliveryStatus variable inside the lastMessage variable using
wait.until (ExpectedConditions.visibilityOfElementLocated (By.locator))

How can I do this?

I don't want to do it using one immense Xpath. Besides, I don't even know how to find the last element with the id body_bubble using Xpath, as the number of these elements is not fixed and always changing.

P.S. For example, in Python we can define WebDriverWait with an element in the constructor instead of driver: WebDriverWait(webelement, self.timeout)

Unfortunately, it does not work in Java.



Solution 1:[1]

Based on the Java documentation on ExpectedConditions, it looks like we can use presenceOfNestedElementLocatedBy to add explicit wait on the child element:

WebDriverWait wait = new WebDriverWait(driver, 15);

WebElement childElem = wait.until(
    ExpectedConditions.presenceOfNestedElementLocatedBy(lastMessage, By.xpath(xpathDeliveryStatus)));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CEH