如何获得硒的webdriver + C#在下拉列表中的所有选项? [英] How to get all options in a drop-down list by Selenium webdriver + c#?

查看:342
本文介绍了如何获得硒的webdriver + C#在下拉列表中的所有选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C#和硒的webdriver。



我知道如何选择/点击在下拉列表中的选项,但我已经在此之前的一个问题。由于是动态生成的下拉列表中,我已经运行的每个案件之前得到列表中的所有选项/值。



有没有人请你告诉我怎么去所有值/从下拉列表中选择。我使用IE浏览器,我没有发现支持的方法来获取C#值/在Selenium.IE命名空间选项的任意类



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

 < TD> 
<选择名称=TIME_ZONE>
<选项值-09:00><脚本> timezone.Alaska< / SCRIPT>< /选项>
<选项值+00:00><脚本> timezone.England< / SCRIPT>< /选项>
<选项值+02:00><脚本> timezone.Greece< / SCRIPT>< /选项>
<选项值+05:30><脚本> timezone.India< / SCRIPT>< /选项>
< /选择>
< TD>

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



我的代码:

  IWebElement ELEM = driver.FindElement( By.XPath(//选择[@名称='TIME_ZONE'])); 
名单,LT; IWebElement>选项= elem.FindElements(By.TagName(选项));



C#只是弹出一个错误:
不能隐式类型的秘密OpenQA.Selenium.IWebElement以System.Collections.Generic.List。一个显式转换存在(是否缺少强制转换?)。



感谢。


解决方案

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



<预类=郎-CS prettyprint-覆盖> IWebElement ELEM = driver.FindElement(By.XPath(//选择[@名称= '时区']));

SelectElement选择列表=新SelectElement(ELEM);
&IList的LT; IWebElement>选项= selectList.Options;

您就可以访问每一个选项作为IWebElement,如:

  IWebElement firstOption =选项[0]; 
Assert.AreEqual(firstOption.GetAttribute(值),-09:00);


I'm new to both C# and 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#.

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?

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?).

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:

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 firstOption = options[0];
Assert.AreEqual(firstOption.GetAttribute("value"), "-09:00");

这篇关于如何获得硒的webdriver + C#在下拉列表中的所有选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆