Java 如何等待,直到没有属性为ready\u to\u send的span标记,或者换句话说,span标记已发送属性

Java 如何等待,直到没有属性为ready\u to\u send的span标记,或者换句话说,span标记已发送属性,java,selenium,selenium-webdriver,xpath,webdriverwait,Java,Selenium,Selenium Webdriver,Xpath,Webdriverwait,我将Selenium与Java一起使用 有许多分区(div)具有相同的类,但范围属性不同 HTML示例: <div class="message_bubble"> <span data-icon="ready_to_send" class=""> ..... ..... </span> </div> // another div with same class="message_bubble" &

我将
Selenium
Java
一起使用

有许多
分区(div)
具有相同的
,但
范围
属性
不同

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
要调用服务员,直到没有属性为“准备发送”的
标记,您可以使用子句和方法诱导WebDriverwait,并且可以使用以下解决方案:

Boolean bool1 = new WebDriverWait(driver, 20).until(ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[@class='message_bubble']/span[@data-icon='ready_to_send']"))));
Boolean bool2 = new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfAllElements(driver.findElements(By.xpath("//div[@class='message_bubble']/span[@data-icon='ready_to_send']"))));
或者,您也可以将incluse WebDriverwait与子句方法一起使用,并且可以使用以下解决方案:

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

是的,这很有帮助。用投票的方式接受答案,这也帮了大忙。投票也是为了给你宝贵的时间、阅读和支持。非常感谢。