【问题标题】:How to select an item from ComboBox using WinAppDriver?如何使用 WinAppDriver 从 ComboBox 中选择一个项目?
【发布时间】:2020-11-04 19:14:56
【问题描述】:

通常对于 web 应用程序,如果我们想从淹死中选择一个选项,我们使用 SelectElement 方法。

但在 Windows 应用程序中,当我尝试使用 SelectElement 方法时,出现以下错误:

OpenQA.Selenium.Support.UI.UnexpectedTagNameException: 元素应该被选中,但却是 ControlType.ComboBox

那么对于 windows 应用,如何从 ComboBox 下拉菜单中选择一个项目?

【问题讨论】:

  • 感谢您更新问题,好先生!

标签: c# appium winappdriver


【解决方案1】:

在 Combobox 下拉菜单中选择项目有两种方式:

  1. 如果元素没有唯一的属性和值,则使用键盘键:

    WindowsElement comboBoxElement = session.FindElementByClassName("ComboBox");
    comboBoxElement.Click();
    comboBoxElement.SendKeys(Keys.Down);
    comboBoxElement.SendKeys(Keys.Enter);
    
  2. 如果它具有唯一的属性和值,则使用下拉列表元素:

    WindowsElement comboBoxElement = session.FindElementByClassName("ComboBox");
    comboBoxElement.Click();
    comboBoxElement.FindElementByAccessibilityId("Light Dismiss").Click(); 
    

【讨论】:

  • 使用 SendKey 不是优雅的方式,因为你必须记住你在组合框列表中的位置,但它是有效的。
【解决方案2】:
    /// <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();
        
    }

【讨论】:

  • 在我尝试这个之前,我无法告诉你我已经解决了多少解决方案。它很有魅力。
【解决方案3】:

您也可以在 SendKeys 上使用组合框项目名称本身:

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

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多