【问题标题】:Find element using explicit wait within another element在另一个元素中使用显式等待查找元素
【发布时间】:2020-03-23 02:33:48
【问题描述】:

我有以下代码:

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 中,我们可以在构造函数中使用元素而不是驱动程序来定义 WebDriverWaitWebDriverWait(webelement, self.timeout)

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

【问题讨论】:

    标签: java android automation appium qa


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!还发现我可以使用这样的东西:wait.until(ExpectedConditions.visibilityOf (lastMessage.findElement (By.xpath(xpathDeliveryStatus))));
    • 很高兴听到这对您有用。 ExpectedConditions 类在所有语言中都非常灵活。
    猜你喜欢
    • 2021-11-28
    • 2019-05-31
    • 2016-01-21
    • 2015-06-02
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 2022-01-24
    相关资源
    最近更新 更多