【问题标题】:Selenium with combo box control and select element带有组合框控件和选择元素的 Selenium
【发布时间】:2018-05-29 15:59:25
【问题描述】:

我尝试自动化的产品有一个来自 .Net 的自定义组合框控件。

该控件的类型为<input>,我将能够仅使用起始字母键入n search。

当我尝试使用 WebDriver 访问 <select> 元素时,它说无法在 <input> 字段上执行操作。

当我尝试在iWebElement 上执行.Sendkeys 时,它只能选择带有起始文本的值。

有没有什么办法可以使用WebDriver来选择使用整个文本的组合框值?

示例 DOM 采用以下格式

<input name="icombobox_Text" tabIndex="7" title="Click to select theValue" class="ComboBox_Normal TxtBox_Css" id="icombobox_Text" accessKey="L" onkeydown="return C28.KeyDown();" onkeyup="return C28.KeyUp();" onkeypress="return C28.KeyPress();" onclick="$_('C28','TextClick')" onfocus="$_('C28','Focus')" onblur="$_('C28','Blur')" onselectstart="$_('C28','SelectStart')" onpaste="return false" oncontextmenu="return C28.KeyRightClick()" type="text" maxLength="255" maxSize="10" minSize="5" AUTOCOMPLETE="off"/>

组合中有 4 个项目。如何选择特定值。?

【问题讨论】:

  • 你能发布一段包含这个项目的 HTML 吗?除非我们能从 HTML 的角度看到它的外观,否则很难说什么
  • Selenium.UI.Support 中的 Select 类仅支持标签 '
  • 现在添加HTML内容

标签: c# selenium selenium-webdriver combobox


【解决方案1】:

我得到了答案。

概念:

  1. 组合控件具有文本框 + 列表(下拉图像)。
  2. 所以首先通过点击找到下拉图片的元素。
  3. 然后使用“SelectElement”类从列表中选择值。

示例代码:

IWebElement iWebelement = driver.FindElement(By.Id("combobox_Text")); //获取文本框的元素

IWebElement iWebelementList = driver.FindElement(By.Id("combobox_List")); //获取List/Drop-down Box的元素

SelectElement 选定 = 新 SelectElement(iWebelementList); //解析列表

iWebelement.SendKeys(Keys.ArrowDown); // 点击下拉图片

selected.SelectByText("一月"); // 使用select元素类来选择值

【讨论】:

    【解决方案2】:

    使用以下代码,从下拉菜单中选择一个选项:

    IWebElement iwe = Driver.FindElement(By.XPath("//li[contains(@class,'..item...') and contains(text(),'"+text+"')]"));           
    
    Thread.Sleep(500);
    
    Actions action = new Actions(Driver);
    Thread.Sleep(500);
    
    action.MoveToElement(iwe).Click().Perform();
    

    【讨论】:

      猜你喜欢
      • 2017-01-21
      • 1970-01-01
      • 2017-08-10
      • 2023-03-14
      • 2022-08-12
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多