簡體   English   中英

如何等待,直到沒有帶有屬性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

我在Java使用Selenium

有許多具有相同class divisions (divs) ,但spans 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發送到服務器時,其span attribute將被sent

如何使驅動程序等待,直到沒有具有span屬性ready_to_send分區,或者換句話說, sent具有span attribute所有分區。

我的無效代碼是:

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

要調用侍者,直到沒有帶有ready_to_send 屬性的 <span>標簽,您可以使用ExpectedConditionsnot子句以及ready_to_send ()方法來誘導WebDriverwait ,並且可以使用以下解決方案:

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

或者,您也可以將歸納式WebDriverwaitExpectedConditions子句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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM