硒中的ExpectedConditions.elementToBeSelected和elementSelectionStateToBe有什么区别?如何使用它?你能举个例子吗?

最佳答案

ElementToBeSelected

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
}

09-30 12:21