I'm selecting multiple options in a "select" tag using selectByIndex() method of Select class in the url [https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html][1] using the below code.
//Selecting multiple options
WebElement multiSelectElement = driver.findElement(By.id("multi-select"));
Select select = new Select(multiSelectElement);
select.selectByIndex(2);
select.selectByIndex(4);
List<String> selectedValues = new ArrayList<String>();
List<WebElement> selectedElements = select.getAllSelectedOptions();
for(WebElement element : selectedElements) {
selectedValues.add(element.getText());
}
//clicking on "Get All Selected"
driver.findElement(By.id("printAll")).click();
WebElement text = driver.findElement(By.xpath(""
+ "//button[@id='printAll']/following-sibling::p"));
//Getting the selected options
String textValue = text.getText();
//split the textValue storing the text after ":" into a variable
String[] s= textValue.split("are :");
System.out.println(s[1]);
The code should print all the selected options. But it is only displaying last selected option. Please correct me.