如何等到没有带有属性 ready_to_send 的 span 标签,或者换句话说,具有已发送属性的 span 标签

How to wait until there is no span tag with attribute ready_to_send or in other words span tag having attribute sent

我正在使用 SeleniumJava

有很多 divisions (divs) 相同 classspans attributes 不同...

示例HTML:

<div class="message_bubble">
    <span data-icon="ready_to_send" class="">
        .....
        .....
    </span>
</div>

// another div with same class="message_bubble"

<div class="message_bubble">
    <span data-icon="sent" class="">
        .....
        .....
    </span>
</div>

// again div with same class="message_bubble"

<div class="message_bubble">
    <span data-icon="received" class="">
        .....
        .....
    </span>
</div>

// There are many divs as such

ready_to_send 发送到服务器时,其范围 attribute 变为 sent

如何让驱动程序等待,直到没有具有跨度属性 ready_to_send 的分区,或者换句话说,所有具有跨度 attribute sent 的分区。

我的非工作代码是:

private Boolean GetStatus()
{
    WebDriverWait UntilSent = new WebDriverWait(driver, 10);

    Boolean Status;

    Status = UntilSent.until(new ExpectedCondition<Boolean>()
    {
        public Boolean apply(WebDriver driver) 
        {
            //int elementCount = driver.findElements(By.xpath("//span[@data-icon='ready_to_send']")).size();

            int elementCount = driver.findElements(By.xpath("//div[@class='message_bubble']/span[@data-icon='ready_to_send']")).size();
            System.out.println("UNSENT count is.... : "+elementCount);

            if (elementCount == 0)
                return true;
            else
                return false;
        }
    });

return Status;
}

要调用 waiter 直到没有 <span> 带有 属性 的标签 ready_to_send 你可以诱导 WebDriverwaitnot clause of ExpectedConditions along with the presenceOfAllElementsLocatedBy() 方法,您可以使用以下解决方案:

Boolean bool1 = new WebDriverWait(driver, 20).until(ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[@class='message_bubble']/span[@data-icon='ready_to_send']"))));

或者,您也可以使用 induce WebDriverwaitExpectedConditions clause invisibilityOfAllElements() 方法,您可以使用以下解决方案:

Boolean bool2 = new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfAllElements(driver.findElements(By.xpath("//div[@class='message_bubble']/span[@data-icon='ready_to_send']"))));

你可以使用这个:

new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfElementsToBeLessThan(By.xpath("//div[@class='message_bubble']/span[@data-icon='ready_to_send']"), 1));

这将等到找不到以下 xpath 下的元素。

注意:您必须添加一些导入:

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

在你的代码中应该是这样的:

private Boolean GetStatus()
{
    try {
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.numberOfElementsToBeLessThan(By.xpath("//div[@class='message_bubble']/span[@data-icon='ready_to_send']"), 1));
        return true;
    }catch (Exception e){
        return false;
    }
}