How to get the selected value from a combobox using Selenium WebDriver (Selenium 2)?
Asked Answered
L

6

10

Assume I have this html code:

<select id="superior" size="1" name="superior">
    <option value=""></option>
    <option value="c.i.e.m.md.Division_1">DIVISION007</option>
    <option selected="selected" value="c.i.e.m.md.Division_$$_javassist_162_119">MyDivision</option>
    <option value="c.i.e.m.md.Division_121">MyDivision4</option>
    <option value="c.i.e.m.md.Division_122">MyDivision5</option>
</select>

So this is a combo box with

id=superior 

and currently value MyDivision is selected.

Using Selenium WebDriver I am trying to get the selected value, but no success.

I tried:

String option = this.ebtamTester.firefox.findElement(By.id(superiorId)).getText();
return option;

But this returns me all the values in the combobox.

Help please?

Edit:

WebElement comboBox = ebtamTester.firefox.findElement(By.id("superior"));
SelectElement selectedValue = new SelectElement(comboBox);
String wantedText = selectedValue.getValue();
Lowman answered 16/8, 2012 at 16:54 Comment(1)
What language are you using? java?Microminiaturization
R
19

This is written in C#, but it shouldn't be hard to transition it over to any other language you're using:

IWebElement comboBox = driver.FindElement(By.Id("superior"));
SelectElement selectedValue = new SelectElement(comboBox);
string wantedText = selectedValue.SelectedOption.Text;

SelectElement requires you to use OpenQA.Selenium.Support.UI, so at the top, type

using OpenQA.Selenium.Support.UI;

Edit:

I suppose for you, instead of 'driver' you would use

IWebElement comboBox = this.ebtamTester.firefox.FindElement(By.Id("superior"));
Richter answered 16/8, 2012 at 17:30 Comment(7)
I am getting an error saying SelectElement consturctor is protected, so I am not allowed to create an instance of it.Lowman
Are you writing in C#, or a different language? Also, can you edit your main post with the code you're testing out now?Richter
Hi, thanks for your help. I am using JAVA. I edited my first post. new SelectElement() won't work because as I said, the constructor seems protected. It works when I try: Select selectedValue = new Select(comboBox); but then, I can't figure out how to get the currently selected text?Lowman
Hello, I made it work like this: WebElement comboBox = ebtamTester.firefox.findElement(By.id("superior")); Select selectedValue = new Select(comboBox); String wantedText = selectedValue.getFirstSelectedOption().getText(); ebtamTester.logger.logText("Selected value from" + selectBoxName, wantedText); Thank you..Lowman
selectedValue.SelectedOption.Text; will get you the text of the selected item. Does anyone know how to get the selected value. I want to test that the value is as I expected, not the text as that can change.Sesquicarbonate
@Sesquicarbonate Peter Kerr provided answer.Lowman
This will return only single selected option in multiselect combobox.Indium
M
9

In Java, the following code should work nicely:

import org.openqa.selenium.support.ui.Select;
Select comboBox = new Select(driver.findElement(By.id("superior")));
String selectedComboValue = comboBox.getFirstSelectedOption().getText();
System.out.println("Selected combo value: " + selectedComboValue);

As MyDivision is selected currently, the above code would print "MyDivision"

Microminiaturization answered 16/1, 2013 at 13:20 Comment(0)
H
4

selectedValue.SelectedOption.Text; will get you the text of the selected item. Does anyone know how to get the selected value.

To get the selected value use

selectedValue.SelectedOption.GetAttribute("value");
Hafiz answered 30/8, 2013 at 15:53 Comment(0)
S
3

To select an option based on the label:

Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
select.deselectAll();
select.selectByVisibleText("Value1");

To get the first selected value:

WebElement option = select.getFirstSelectedOption()
Sealer answered 17/8, 2012 at 5:40 Comment(2)
I tried option.toString() and I got: [[[[Firefox: firefox on UNIX (a88d1b9f-93d8-4140-9693-5a91d59b57f6)] -> id: superior]] -> tag name: option]Lowman
It turns out I need to use .getText. Thanks.Lowman
H
3

Using XPath in c#

  string selectedValue=driver.FindElement(By.Id("superior")).FindElements(By.XPath("./option[@selected]"))[0].Text;
Handbook answered 1/3, 2013 at 13:57 Comment(0)
M
-1

Based off @Micheal's answer this would be easier to work with following command:

string  selectedValue = new SelectElement(driver.FindElement(By.Id("Year"))).SelectedOption.Text;
Mackinaw answered 17/2, 2022 at 12:5 Comment(2)
Thank you for improving on an existing answer. If you believe your information is better as a new answer than a comment on the existing answer, I recommend to edit to ensure your answer is full/complete and does not require reading a different answer and combining it with your new information. Usually, I also leave a comment on that answer to mention that I have posted an improved answer.Sacristan
In this case, I believe this is an almost exact duplicate of the existing answer, but with variables inlined onto a single line (albeit with an incorrect string "Year" instead of "superior") and less information. I recommend to use comments for this purpose instead of answers.Sacristan

© 2022 - 2024 — McMap. All rights reserved.