selenium 中的 ExpectedConditions.elementToBeSelected 和 elementSelectionStateToBe 有什么区别?如何使用它?能举个例子吗?
最佳答案
要选择的元素
public static ExpectedCondition<java.lang.Boolean> elementToBeSelected(WebElement element)
ElementSelectionStateToBe
public static ExpectedCondition<java.lang.Boolean> elementSelectionStateToBe(WebElement element, boolean selected)
正如您从方法签名中看到的,elementSelectionStateToBe
接收 boolean
作为参数。您可以通过传递参数来检查元素是否被选中,而在 elementToBeSelected
中则需要捕获异常来检查元素是否未被选中。
检查元素是否被选中
// waits for the element to be selected
wait.until(ExpectedCondition.elementSelectionStateToBe(element, true));
// waits for the element to be selected
wait.until(ExpectedCondition.elementToBeSelected(element));
检查元素是否未被选择
// waits for the element **not** to be selected
wait.until(ExpectedCondition.elementSelectionStateToBe(element, false));
try {
// waits for the element to be selected
wait.until(ExpectedCondition.elementToBeSelected(element));
}
catch (TimeOutException)
{
// the element is not selected
}
关于selenium - Selenium 中 ExpectedConditions.elementToBeSelected 和 elementSelectionStateToBe 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35208638/