LexSav
LexSav

Reputation: 454

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.

Upvotes: 1

Views: 323

Answers (1)

CEH
CEH

Reputation: 5909

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)));

Upvotes: 2

Related Questions