Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ExpectedConditions.elementToBeSelected and elementSelectionStateToBe in selenium

What is the Difference between ExpectedConditions.elementToBeSelected and elementSelectionStateToBe in selenium? How to use it? Can you give any example?

like image 591
Selenium Avatar asked Aug 09 '26 00:08

Selenium


1 Answers

ElementToBeSelected

public static ExpectedCondition<java.lang.Boolean> elementToBeSelected(WebElement element)

ElementSelectionStateToBe

public static ExpectedCondition<java.lang.Boolean> elementSelectionStateToBe(WebElement element, boolean selected)

As you can see from the methods signature elementSelectionStateToBe receives boolean as parameter. You can use this to check if the element is selected or not by passing parameter, while you need to catch an exception to check if the element isn't selected in elementToBeSelected.

To check if element is selected

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

To check if element is not selected

// 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
}
like image 126
Guy Avatar answered Aug 18 '26 12:08

Guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!