首页 文章

如何使用C#在Selenium WebDriver的下拉列表中获取所有选项?

提问于
浏览
12

我是C#和Selenium WebDriver的新手 .

我知道如何在下拉列表中选择/单击某个选项,但在此之前我遇到了问题 . 由于下拉列表是动态生成的,因此在运行每个案例之前,我必须从列表中获取所有选项/值 .

有没有人告诉我如何从下拉列表中获取所有值/选项 . 我正在使用IE,我没有找到任何支持在Selenium.IE名称空间中为C#获取值/选项的方法的类 .

我的例子:列表包含几个时区:

<TD>
  <select name = "time_zone">
    <option value "-09:00"><script>timezone.Alaska</script></option>
    <option value "+00:00"><script>timezone.England</script></option>
    <option value "+02:00"><script>timezone.Greece</script></option>
    <option value "+05:30"><script>timezone.India</script></option>
  </select>
<TD>

这是IE页面中的下拉列表以及如何获取动态生成的时区列表?

我的代码:

IWebElement elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));
List<IWebElement> options = elem.FindElements(By.TagName("option"));

C#只是弹出一个错误:无法隐式将'OpenQA.Selenium.IWebElement'类型转换为'System.Collections.Generic.List' . 存在显式转换(您是否错过了演员?) .

谢谢 .

11 回答

  • 1

    您可以尝试使用 OpenQA.Selenium.Support.UI.Selected 命名空间中的WebDriver.Support SelectElement 来访问选择列表的选项列表:

    IWebElement elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));
    
    SelectElement selectList = new SelectElement(elem);
    IList<IWebElement> options = selectList.Options;
    

    然后,您可以将每个选项作为IWebElement访问,例如:

    IWebElement firstOption = options[0];
    Assert.AreEqual(firstOption.GetAttribute("value"), "-09:00");
    
  • 3
    Select select = new Select(driver.findElement(By.id("searchDropdownBox")));
    
    select.getOptions();//will get all options as List<WebElement>
    
  • 8

    确保引用WebDriver.Support.dll程序集以获取对OpenQA.Selenium.Support.UI.SelectElement下拉帮助程序类的访问权限 . 有关其他详细信息,请参阅this thread .

    编辑:在此屏幕截图中,您可以看到我可以很好地获得选项 . 在创建新的InternetExplorerDriver时,IE是否会打开?
    Screenshot

  • 0

    以下是Java中的代码,用于获取下拉列表中的所有选项 .

    WebElement sel = myD.findElement(By.name("dropdown_name"));
    List<WebElement> lists = sel.findElements(By.tagName("option"));
        for(WebElement element: lists)  
        {
            String var2 = tdElement.getText();
            System.out.println(var2);
        }
    

    希望它对某人有帮助 .

  • 0

    你可以使用 selenium.Support 来使用 SelectElement 类,这个类有一个你正在寻找的属性"Options",我创建了一个扩展方法来将你的web元素转换为select元素

    public static SelectElement AsDropDown(this IWebElement webElement)
    {
        return new SelectElement(webElement);
    }
    

    然后你可以像这样使用它

    var elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));
    var options = elem.AsDropDown().Options
    
  • 2

    使用 IList<IWebElement> 而不是 List<IWebElement> .

    例如:

    IList<IWebElement> options = elem.FindElements(By.TagName("option"));
    foreach (IWebElement option in options)
    {
        Console.WriteLine(option.Text);
    }
    
  • 0
    To get all the dropdown values you can use List.
    
    List<string> lstDropDownValues = new List<string>();
    int iValuescount = driver.FindElement(By.Xpath("\html\....\select\option"))
    
    for(int ivalue = 1;ivalue<=iValuescount;ivalue++)
     {
      string strValue = driver.FindElement(By.Xpath("\html\....\select\option["+ ivalue +"]"));
      lstDropDownValues.Add(strValue); 
     }
    
  • 7
    WebElement drop_down =driver.findElement(By.id("Category"));
    Select se = new Select(drop_down);
    for(int i=0 ;i<se.getOptions().size(); i++)
    System.out.println(se.getOptions().get(i).getAttribute("value"));
    
  • 0
    WebElement element = driver.findElement(By.id("inst_state"));
            Select s = new Select(element);
            List <WebElement> elementcount = s.getOptions();
    
            System.out.println(elementcount.size());
            for(int i=0 ;i<elementcount.size();i++)
            {
                String value = elementcount.get(i).getText();
                System.out.println(value);
    
            }
    
  • 0

    要通过Selenium WebDriver C#获取下拉列表中的所有选项:

    SelectElement TimeZoneSelect = new SelectElement(driver.FindElement(By.Name("time_zone")));
    IList<IWebElement> ElementCount = TimeZoneSelect.Options;
    int ItemSize = ElementCount.Count;
    
    for (int i = 0; i < ItemSize; i++)
    {
        String ItemValue = ElementCount.ElementAt(i).Text;
        Console.WriteLine(ItemValue);
    }
    
  • 15

    这似乎是一个演员例外 . 您可以尝试将结果转换为列表,即 elem.findElements(xx).toList 吗?

相关问题