Selenium中ExpectedConditions.elementToBeSelected和elementSelectionStateToBe之间的区别
问题描述:
Selenium中ExpectedConditions.elementToBeSelected和elementSelectionStateToBe之间的区别是什么?如何使用它?你能举个例子吗?Selenium中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
}
只是为了一点点加入到这样的回答: ExpectedCondition.elementToBeSelected(元)实际调用ExpectedCondition。 elementSelectionStateToBe(element,true): 'public static ExpectedCondition elementToBeSelected(final WebElement element){ return elementSelectio nStateToBe(element,true); }'[src](https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/ui/ExpectedConditions.java) –
NotInventedHere