learningQA
learningQA

Reputation: 125

How to get the text of the selected options from a multi select list using Selenium and Java

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.

Upvotes: 2

Views: 2247

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193088

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?

Upvotes: 3

frianH
frianH

Reputation: 7563

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

Upvotes: 1

Related Questions