[英]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>
標簽,您可以使用ExpectedConditions的not子句以及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']"))));
或者,您也可以將歸納式WebDriverwait與ExpectedConditions子句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.