1

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.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
learningQA
  • 115
  • 1
  • 9
  • there is something wrong with the application as it is only showing the last selected value after hitting Get All Selected button. Your code is working as expected. – yudi2312 Nov 15 '19 at 07:56
  • yes. It is displaying all the selected when you do manually. while executing the script only last selected is being displayed. I've asked this question to know if there is anything wrong in my code. – learningQA Nov 15 '19 at 10:28
  • Do you want to print all the selected options or you want to print the text in front of label `Options selected are :` ? – yudi2312 Nov 15 '19 at 10:43
  • When you select multiple options, the selected options will be displayed there. I want to fetch and display them. – learningQA Nov 15 '19 at 11:50
  • @debanjanb Don't roll back my edits. This is a Selenium WebDriver question, not a Selenium question. The OP isn't asking anything about XPath or WebDriverWait... why are you adding those tags? They aren't relevant to the question. – JeffC Nov 15 '19 at 15:08

2 Answers2

2

To extract the text of selected elements with in the Multi Select List you need to use induce WebDriverWait for the visibilityOfElementLocated() along with Actions class and you can use the following Locator Strategies:

  • Code Block:

    public class A_demo 
    {
        public static void main(String[] args) throws Exception 
        {
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
            options.setExperimentalOption("useAutomationExtension", false);
            WebDriver driver = new ChromeDriver(options);
            driver.get("https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");
            ((JavascriptExecutor)driver).executeScript("return arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[text()='Multi Select List Demo']"))));
            WebElement california = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='multi-select']//option[@value='California']")));
            WebElement ohio = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='multi-select']//option[@value='Ohio']")));
            WebElement washington = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='multi-select']//option[@value='Washington']")));
            new Actions(driver).moveToElement(california).click().build().perform();
            new Actions(driver).keyDown(Keys.CONTROL).click(ohio).keyUp(Keys.CONTROL).build().perform();
            new Actions(driver).keyDown(Keys.CONTROL).click(washington).keyUp(Keys.CONTROL).build().perform();
            new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='printAll' and @value='Print All']"))).click();
            System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@class='getall-selected']"))).getText());
            driver.quit();
        }
    }
    
  • Console Output:

    Options selected are : California,Ohio,Washington
    

You can find a Python based similar discussion in How to select multiple options from multi select list using Selenium-Python?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

Actually you are print by Get All Selected result, it seem like there are something wrong with the apps. But you can extract your String List as follow:

List<String> selectedValues = new ArrayList<String>();
List<WebElement> selectedElements = select.getAllSelectedOptions();

for(WebElement element : selectedElements) {
    selectedValues.add(element.getText());
}

for(String text: selectedValues) {
    System.out.println(text);
}
frianH
  • 7,295
  • 6
  • 20
  • 45