在另一个元素中使用显式等待查找元素

Find element using explicit wait within another element

我有以下代码:

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

我想做同样的事情,只是我想使用显式等待来查找 deliveryStatus 变量。
所以我想使用
wait.until (ExpectedConditions.visibilityOfElementLocated (By.locator))lastMessage 变量中找到 deliveryStatus 变量

我该怎么做?

我不想使用一个巨大的 Xpath 来完成它。此外,我什至不知道如何使用 Xpath 找到 ID 为 body_bubble 的最后一个元素,因为这些元素的数量不固定并且总是在变化。

P.S. 例如,在 Python 中我们可以定义 WebDriverWait构造函数中的元素而不是驱动程序: WebDriverWait(webelement, self.timeout)

遗憾的是,它在 Java 中不起作用。

基于Java documentation on ExpectedConditions,看起来我们可以使用presenceOfNestedElementLocatedBy来添加对子元素的显式等待:

WebDriverWait wait = new WebDriverWait(driver, 15);

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