How to select an item from ComboBox using WinAppDriver?

Viewed 4821

Generally for web application if we want to select an option from drown we use SelectElement method.

But in Windows application, when I tried to use SelectElement method, I got a below error:

OpenQA.Selenium.Support.UI.UnexpectedTagNameException: Element should have been select but was ControlType.ComboBox

So for windows application, How to select a item from ComboBox dropdown ?

3 Answers

There are two ways to select the items in Combobox dropdown:

  1. By Using Keyboard keys if elements don't have unique attribute and value:

    WindowsElement comboBoxElement = session.FindElementByClassName("ComboBox");
    comboBoxElement.Click();
    comboBoxElement.SendKeys(Keys.Down);
    comboBoxElement.SendKeys(Keys.Enter);
    
  2. By using drop down list Element if it has unique attribute and value:

    WindowsElement comboBoxElement = session.FindElementByClassName("ComboBox");
    comboBoxElement.Click();
    comboBoxElement.FindElementByAccessibilityId("Light Dismiss").Click(); 
    
    /// <summary>
    /// select an item from a combobox 
    /// </summary>
    /// <param name="element">the combo box element</param>
    /// <param name="index">the index of the item in the combobox list</param>
    public void SelectComboboxItem(AppiumWebElement element,int index)
    {
        element.Click();

        var comboBoxItems = element.FindElementsByClassName("ListBoxItem");

        new Actions(element.WrappedDriver).MoveToElement(comboBoxItems[index]).Click().Perform();
        
    }

You can use also combobox item name itself on SendKeys:

WindowsElement comboBoxElement = session.FindElementByClassName("ComboBox");
comboBoxElement.SendKeys("combobox item name");
Related