Dav*_* Wu 16 c# selenium selenium-webdriver drop-down-menu
我是C#和Selenium WebDriver的新手.
我知道如何在下拉列表中选择/单击某个选项,但在此之前我遇到了问题.由于下拉列表是动态生成的,因此在运行每个案例之前,我必须从列表中获取所有选项/值.
有没有人告诉我如何从下拉列表中获取所有值/选项.我正在使用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>
Run Code Online (Sandbox Code Playgroud)
这是IE页面中的下拉列表以及如何获取动态生成的时区列表?
我的代码:
IWebElement elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));
List<IWebElement> options = elem.FindElements(By.TagName("option"));
Run Code Online (Sandbox Code Playgroud)
C#只是弹出一个错误:无法隐式将'OpenQA.Selenium.IWebElement'类型转换为'System.Collections.Generic.List'.存在显式转换(您是否错过了演员?).
谢谢.
Poc*_*ews 17
您可以尝试使用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;
Run Code Online (Sandbox Code Playgroud)
然后,您可以将每个选项作为IWebElement访问,例如:
IWebElement firstOption = options[0];
Assert.AreEqual(firstOption.GetAttribute("value"), "-09:00");
Run Code Online (Sandbox Code Playgroud)
小智 8
Select select = new Select(driver.findElement(By.id("searchDropdownBox")));
select.getOptions();//will get all options as List<WebElement>
Run Code Online (Sandbox Code Playgroud)