如何使Selenium网络驱动程序等待元素将其属性更改为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));

我收到错误:

方法 until_not(预期条件( 未定义 类型 WebDriverWait

另外,即使我将"until_not"更改为"直到",我也会收到此警告:

方法textToBePresentInElement(By,String(从类型中 预期条件已弃用

我该怎么做?

如果您正在检查属性值,请检查文本对您没有帮助,这是两回事。

您可以为此组合notattributeToBe预期条件

wait.until(ExpectedConditions.not(ExpectedConditions.attributeToBe(By.xpath(path), "data-id", sId)));

您可以尝试使用 not 来自预期条件,例如:

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

最新更新