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

C# 如何通过SeleniumWebDriver使用C获取下拉列表中的所有选项?,c#,drop-down-menu,selenium,selenium-webdriver,C#,Drop Down Menu,Selenium,Selenium Webdriver,我对C和SeleniumWebDriver都是新手 我知道如何在下拉列表中选择/单击某个选项,但在此之前我遇到了一个问题。由于下拉列表是动态生成的,因此在运行每个案例之前,我必须从列表中获取所有选项/值 有没有人能告诉我如何从下拉列表中获取所有值/选项。我正在使用IE,但在Selenium.IE命名空间中找不到任何支持方法获取C的值/选项的类 我的例子是: 列表包含多个时区: <TD> <select name = "time_zone"> <optio

我对C和SeleniumWebDriver都是新手

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

有没有人能告诉我如何从下拉列表中获取所有值/选项。我正在使用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”。存在显式转换。是否缺少强制转换


谢谢。

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

编辑:在这个截图中,你可以看到我可以很好地获得选项。当你创建一个新的InternetExplorerDriver时,IE开放了吗?

这似乎是演员阵容的一个例外。你能试着把结果转换成列表吗
i、 e.elem.findElementsxx.toList?

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

IWebElement elem=driver.FindElementBy.XPath//select[@name='time_zone']; SelectElement selectList=新建SelectElementelem; IList options=selectList.options; 然后,您可以作为IWebElement访问每个选项,例如:

IWebElement firstOption = options[0];
Assert.AreEqual(firstOption.GetAttribute("value"), "-09:00");
使用IList而不是List

例如:

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

下面是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);
    }
希望它可能对其他人有所帮助。

您可以使用selenium。支持使用SelectElement类,该类具有您正在寻找的属性选项,我创建了一个扩展方法来将web元素转换为SelectElement

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

select.getOptions();//will get all options as List<WebElement>
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

要通过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);
}

我已经在参考中添加了WebDriver.dll和WebDriver.Support.dll。无论如何,还是要谢谢你。我仍在寻找解决方案。如果使用隐式类型,代码应该可以工作:var options=elem.FindElementsBy.tagnamepoption;jmac,谢谢你。我尝试了,但它仍然返回节点的类型,而不是选项的值/文本。我也尝试过:var tz=elem.FindElmentxx.Text.ToString;它返回,只是一个空格。你知道为什么吗?是的,我试过这样做。但是当我打印列表的元素时,列表[I]的所有元素都是OpenQA.Selenium.Remote.RemoteWebElement,这是类型,但不是值。谢谢大家。我在Selenium命名空间中找到了一个名为GetSelectOptions的方法,但我不知道如何将它与类IWebElement或InternetExplorerWebDriver一起使用。
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
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);
}