3

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 ?

Ankit Rathi
  • 109
  • 1
  • 9

5 Answers5

5

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(); 
    
Ankit Rathi
  • 109
  • 1
  • 9
3
    /// <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();
        
    }
Amir Touitou
  • 3,141
  • 1
  • 35
  • 31
  • Can't tell you how many solutions I have worked through until I tried this. It worked a charm. – cw84 Sep 08 '21 at 16:02
3

You can use also combobox item name itself on SendKeys:

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

Try doing this:

comboBoxElement.FindElement(By.Name("Item Text To Select")).Click();

Somehow I could not get success using any of the above suggested items.

Kim Smith
  • 105
  • 8
0

I found that by selecting the ComboBox and then sending it the first character of an item in the dropdown list I could reliably select the list item I wanted (but all my list items started with a different first character)

I had 2 ComboBoxes on by form. Here's the code (just 1 line) First ComboBox the items is the dropdown list were "Pairs" & "Individuals"

driver.FindElementByAccessibilityId("Cb_Class").SendKeys("P");

2nd Combobox items were "Match Points" and "XIMPs"

driver.FindElementByAccessibilityId("Cb_ScoringMethod").SendKeys("M");

Tony H
  • 61
  • 3