[英]How do I make Selenium webdriver to wait for an element to change its attribute to something else in Java
以下是我的要素;
String sId = driver.findElement(By.xpath(path)).getAttribute("data-id");
由於現在屬性值存儲在“ sId”中,因此我現在需要告訴Selenium等待,直到data-id屬性值不等於 sID。 我沒有運氣嘗試過下面的代碼:
wait.until_not(ExpectedConditions.textToBePresentInElement(By.xpath(path), sId));
我得到錯誤:
對於類型WebDriverWait,未定義方法until_not(ExpectedCondition)
另外,即使我將“ until_not”更改為“ until”,也會收到此警告:
不推薦使用類型為ExpectedConditions的方法textToBePresentInElement(By,String)
我該怎么做?
如果您正在檢查屬性值,那么檢查文本將無濟於事,這是兩件事。
您可以將not
和attributeToBe
ExpectedConditions結合在一起
wait.until(ExpectedConditions.not(ExpectedConditions.attributeToBe(By.xpath(path), "data-id", sId)));
您可以嘗試不使用“預期”條件,例如:
wait.until(ExpectedConditions.not(ExpectedConditions.textToBePresentInElement(<element>, <elementtext>)));
或者,您也可以使用預期條件隱身方法:
wait.until(ExpectedConditions.invisibilityOfElementWithText(locator, text));
您可以嘗試以下代碼
String sId = driver.findElement(By.xpath(path)).getAttribute("data-id");
boolean expectedCondition = false;
boolean timeOut = false;
int second = 1;
do {
try {
//timeout for 60 seconds
if(second>60) {
timeOut = true;
}
String expectedId = driver.findElement(By.xpath(path)).getAttribute("data-id");
if(! expectedId.equals(sId)) {
expectedCondition=true;
}else {
second++;
}
} catch (Exception e) {
TimeUnit.SECONDS.sleep(1);
second++;
}
}while(expectedCondition==false && timeOut==false);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.