简体   繁体   English

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

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

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'. C# 只是弹出一个错误:无法将类型“OpenQA.Selenium.IWebElement”隐式转换为“System.Collections.Generic.List”。 An explicit conversion exists (are you missing a cast?).存在显式转换(您是否缺少演员表?)。

thanks.谢谢。

You can try using the WebDriver.Support SelectElement found in OpenQA.Selenium.Support.UI.Selected namespace to access the option list of a select list: 您可以尝试使用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;

You can then access each option as an IWebElement, such as: 然后,您可以将每个选项作为IWebElement访问,例如:

IWebElement firstOption = options[0];
Assert.AreEqual(firstOption.GetAttribute("value"), "-09:00");
Select select = new Select(driver.findElement(By.id("searchDropdownBox")));

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

Make sure you reference the WebDriver.Support.dll assembly to gain access to the OpenQA.Selenium.Support.UI.SelectElement dropdown helper class. 确保引用WebDriver.Support.dll程序集以获取对OpenQA.Selenium.Support.UI.SelectElement下拉帮助程序类的访问权限。 See this thread for additional details. 请参阅此主题以获取更多详细信

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? 在创建新的InternetExplorerDriver时,IE是否会打开? 截图

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. 希望它对某人有帮助。

You can use selenium.Support to use the SelectElement class, 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类,这个类有一个你正在寻找的属性“Options”,我创建了一个扩展方法来将你的web元素转换为select元素

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

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

        }

To get all options in a drop-down list by Selenium WebDriver C#: 要通过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);
}
 Select s = new Select(driver.findElement(By.xpath("<xpath>")));
 // getting the list of options in the dropdown with getOptions()
 List <WebElement> op = s.getOptions();
 int size = op.size();
 for(int i =0; i<size ; i++)
 {
    String options = op.get(i).getText();
    System.out.println(options);
 }

It seems to be a cast exception. 这似乎是一个演员例外。 Can you try converting your result to a list ie elem.findElements(xx).toList ? 您可以尝试将结果转换为列表,即elem.findElements(xx).toList吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM