我有以下代码:
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)));
关于java - 在另一个元素中使用显式等待查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59071847/