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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9562853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:53:51  来源:igfitidea点击:

How to get all options in a drop-down list by Selenium WebDriver using C#?

c#drop-down-menuseleniumselenium-webdriver

提问by David Wu

I'm new to both C# and Selenium WebDriver.

我是 C# 和 Selenium WebDriver 的新手。

I know how to select/click on an option in a drop-down list, but I've a problem before that. Since the drop-down list is dynamically generated, I have to get all options/values from the list before running each case.

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

Is there anyone kindly tell me how to get all values/options from a drop-down list. I'm using IE and I didn't find any class which supports method to get values/options in Selenium.IE namespace for C#.

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

My example: A list contains several time zones:

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

<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>

This is a drop-down list in an IE page and how to get the dynamically generated time zone list?

这是一个IE页面的下拉列表,如何获取动态生成的时区列表?

My code:

我的代码:

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

C# just pops an Error: Cannot implicitly covert type 'OpenQA.Selenium.IWebElement' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?).

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

thanks.

谢谢。

回答by Jonathan McIntire

Make sure you reference the WebDriver.Support.dll assembly to gain access to the OpenQA.Selenium.Support.UI.SelectElement dropdown helper class. See this threadfor additional details.

确保您引用 WebDriver.Support.dll 程序集以访问 OpenQA.Selenium.Support.UI.SelectElement 下拉帮助程序类。有关其他详细信息,请参阅此线程

Edit: In this screenshot, you can see that I can get the options just fine. Is IE opening up when you create a new InternetExplorerDriver? Screenshot

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

回答by niharika_neo

It seems to be a cast exception. Can you try converting your result to a list i.e. elem.findElements(xx).toList?

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

回答by PocketDews

You can try using the WebDriver.Support SelectElementfound in OpenQA.Selenium.Support.UI.Selectednamespace to access the option list of a select list:

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

IWebElement elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));

SelectElement selectList = new SelectElement(elem);
IList<IWebElement> options = selectList.Options;

You can then access each option as an IWebElement, such as:

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

IWebElement firstOption = options[0];
Assert.AreEqual(firstOption.GetAttribute("value"), "-09:00");

回答by shamp00

Use IList<IWebElement>instead of List<IWebElement>.

使用IList<IWebElement>代替List<IWebElement>

For instance:

例如:

IList<IWebElement> options = elem.FindElements(By.TagName("option"));
foreach (IWebElement option in options)
{
    Console.WriteLine(option.Text);
}

回答by chivas_hvn

Here is code in Java to get all options in dropdown list.

这是 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);
    }

Hope it may helpful to someone.

希望它可以对某人有所帮助。

回答by Anton

Select select = new Select(driver.findElement(By.id("searchDropdownBox")));

select.getOptions();//will get all options as List<WebElement>

回答by Madhu

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); 
 }

回答by vivek k

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"));

回答by Suman Verma

 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);

        }

回答by hiram acebo

You can use selenium.Supportto use the SelectElementclass, this class have a property "Options" that is what you are looking for, I created an extension method to convert your web element to a select element

您可以使用selenium.Support使用SelectElement类,这个类有一个属性“选项”这是你在找什么,我创建了一个扩展方法到您的网络元素转换为选择要素

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

then you could use it like this

那么你可以像这样使用它

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