user1834699
user1834699

Reputation: 21

How to read all the list of values present in the drop down using selenium webdriver java?

Please help me in reading all the list of values present under my Goal Type drop down present in the Salesforce application:

HTML:

<select id="00N20000002gQI3" name="00N20000002gQI3" tabindex="3">
    <option value="">--None--</option>
    <option value="Asset Finance">Asset Finance</option>
    <option value="Barcap">Barcap</option>
    <option value="Barclaycard">Barclaycard</option>
    <option value="Barclays Wealth">Barclays Wealth</option>
    <option value="BGI">BGI</option>
    <option value="Cash Management">Cash Management</option>
    <option value="DCM">DCM</option>
    <option value="Debt">Debt</option>
    <option value="Deposit">Deposit</option>
    <option value="ECM">ECM</option>
    <option value="ESHLA">ESHLA</option>
    <option value="Financial Need Assessment">Financial Need Assessment</option>
    <option value="FX">FX</option>
    <option value="Generic">Generic</option>
    <option value="Individual Relationship">Individual Relationship</option>
    <option value="M&amp;A">M&amp;A</option>
    <option value="Managing Impairment">Managing Impairment</option>
    <option value="Managing RWAs">Managing RWAs</option>
    <option value="Marketing">Marketing</option>
    <option value="Overall Relationship">Overall Relationship</option>
    <option value="RSG">RSG</option>
    <option value="Sales Finance">Sales Finance</option>
    <option value="Trade">Trade</option>
</select>

Please let me know how can I read all the values present in the drop down... If you can share the code that will be a great help.

Upvotes: 0

Views: 8075

Answers (3)

Nora
Nora

Reputation: 1482

if you use this, it should return an array of all the option elements:

element_array = driver.findElement(By.xpath("//select[@id='00N20000002gQI3']/option"))

So you can just extract the text of each element from this array. There's no need to click on the elements.

element_array[1].text
element_array[2].text

and etc.

Upvotes: 0

djangofan
djangofan

Reputation: 29669

Here is a method I have used before to select an option by value:

public static void selectInDropdownByValue(WebElement we, String val) {
    Select dropDown = new Select(we);           
    List<WebElement> theseOptions = dropDown.getOptions();
    for(WebElement option:theseOptions){
        if(option.getAttribute("value").equals(val)){
            option.click();  
        }
    }   
}

Sometimes, with some menus, this won't be enough. In that case you need to use a WebDriver "Action" to physically move the mouse and click the mouse button.

Upvotes: 2

niharika_neo
niharika_neo

Reputation: 8531

You can use getOptions function of Select class. Loop through the list of webelements received and call getText to get the visible options..something like

Select sel = new Select(driver.findElement("yourlocator");
List of WebElements lst = sel.getOptions();
//iterate list with getText

Upvotes: 1

Related Questions