2

我正在Selenium使用Java.

有很多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.

如何让驱动程序等到没有具有 span 属性ready_to_send的部门,或者换句话说,所有部门都具有 span 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;
}
4

2 回答 2

2

To invoke a waiter until there is no <span> tag with attribute ready_to_send you can induce WebDriverwait with the not clause of ExpectedConditions along with the presenceOfAllElementsLocatedBy() method and you can use the following solution:

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

Alternatively, you can also use induce WebDriverwait with the ExpectedConditions clause invisibilityOfAllElements() method and you can use the following solution:

Boolean bool2 = new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfAllElements(driver.findElements(By.xpath("//div[@class='message_bubble']/span[@data-icon='ready_to_send']"))));
于 2018-07-13T12:27:44.387 回答
1

You can use this:

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

this will wait until no elements under following xpath will be found.

Note: you have to add some imports:

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

in your code it would be like this:

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;
    }
}
于 2018-07-13T12:27:50.227 回答