Java-Selenium library for operating Select2 elements
This library can be integrated into Java-Selenium based framework for effective and efficient usage of Select2 element, similar to default selenium.webdriver.support.select implementation
For maven based project add the below dependency
<dependency>
<groupId>io.github.eotsevych</groupId>
<artifactId>select2-java-selenium</artifactId>
<version>1.1.4</version>
</dependency>For gradle based project add the below dependency
implementation 'io.github.eotsevych:select2-java-selenium:1.1.4'
When working with Selenium WebDriver, the default class for interacting with select tags fell short in handling custom Select2 elements on webpages. Despite the commonality in the structure of Select2 elements across different websites, the standard WebDriver class was unable to effectively manage hidden select elements present in the DOM.
To address this issue, a specialized Select2 Java-Selenium library was developed. This library provides an efficient and effective solution for interacting with Select2 elements by overcoming the limitations of the default WebDriver class. The decision to create a custom library was guided by the recognition of a shared structure among Select2 elements on various websites, enabling the development of a generic solution.
WebElement selectElement = driver.findElement(By.name("selectomatic"));
Select select = new Select(selectElement);
WebElement selectElement = driver.findElement(By.name("selectomatic"));
Select2 select = new Select2(selectElement);
- Simple to well-known WebDriver implementation
- Wider functionality range
- Open source
selectByText- Select option by visible text- As input argument receive both
StringorList<String>in case multiple selection
- As input argument receive both
selectByIndex- Select option position number- Counting starts from
1 - As input argument receive both
intorint[]in case multiple selection
- Counting starts from
isOptionDisabledByText- returnbooleanstatus of option's disablesisOptionPresentByText- returnbooleanstatus of option's presentsremoveSelectedOption- remove selected option from single selectremoveAllSelectedOptions- remove all selected options from multi selectremoveSelectedOptions- remove selected options from multi select according toList<String> selectedOptionsgetSelectedOptionText- return text from selected option asTgetMultiSelectedOptionsText- - return text from all selected option asList<T>
Most of select method have additional boolean... isOpened argument. It's designed for cases, when your Select2 element is hidden by any button, and for expanding the option list, you need to click on the button first.
And after clicking at button the option list becase vibile
Unfortunately, such kind of clicking cannot be handled with build-in solution because of unique locator of your element. Solution required to click on the button as additional step
WebElement buttonElement = driver.findElement(By.id("buttonId"));
buttonElement.click()
WebElement selectElement = driver.findElement(By.name("selectomatic"));
Select2 select = new Select2(selectElement);
select.selectByText(text, true)
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.eotsevych.select2.Select2;
public class Select2Example {
public static void main(String[] args) {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-gpu", "--window-size=1920,1200", "--ignore-certificate-errors", "--disable-extensions", "--no-sandbox", "--disable-dev-shm-usage");
WebDriver driver = new ChromeDriver(chromeOptions);
WebDriverWait wait = new WebDriverWait(driver, 5);
// Locate your Select2 element
WebElement selectElement = driver.findElement(By.id("yourSelect2ElementId"));
// Initialize Select2 instance
Select2 select2 = new Select2(selectElement, wait);
// Example: Select options by text
select2.selectByText("Option1", true, true);
// Example: Verify if an option is disabled
boolean isDisabled = select2.isOptionDisabledByText("Option2", false);
// Example: Get list of selected options
List<String> selectedOptions = select2.getMultiSelectedOptionsText();
// Perform additional actions as needed...
// Close the browser
driver.quit();
}
}

