2012-03-05 61 views
11

我是C#和Selenium WebDriver的新手。如何通过Selenium WebDriver使用C#获取下拉列表中的所有选项?

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

有没有人亲切地告诉我如何从下拉列表中获取所有值/选项。我使用的是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'。存在明确的转换(您是否缺少演员?)。

感谢。

回答

6

确保您引用WebDriver.Support.dll程序集以获得对OpenQA.Selenium.Support.UI.SelectElement下拉帮助程序类的访问权限。有关更多详细信息,请参见this thread

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

+0

我已经添加WebDriver.dll和WebDriver.Support.dll到参考。无论如何, – 2012-03-05 08:35:11

+0

,谢谢你们一切。我仍在寻找解决方案。 – 2012-03-05 08:41:13

+0

如果你使用隐式类型,你的代码应该可以工作:var options = elem.FindElements(By.TagName(“option”)); – 2012-03-05 16:54:33

0

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

+0

是的,我尝试过这种方式。但是当我打印List的元素时,List [i]的所有元素都是“OpenQA.Selenium.Remote.RemoteWebElement”,这是类型而不是值。 – 2012-03-05 15:12:30

+0

谢谢你们一切。我在Selenium命名空间中找到了一个名为“GetSelectOptions”的方法,但我没有如何将它与IWebElement类或InternetExplorerWebDriver一起使用。 – 2012-03-05 15:14:01

11

您可以尝试使用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; 

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

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

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

例如:Java中

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

下面是代码来获得在下拉列表中的所有选项。

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

希望对某人有帮助。

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

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

     } 
2

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

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 
0

要由硒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); 
} 
相关问题