我在使用 WebDriver 的 C# 绑定从下拉列表中选择值时遇到了困难。我过去既没有研究过 C# 也没有研究过 WebDriver。我正在使用 WebDriver - Selenium-dotnet2.0b3 和 Visual Studio C# 2010 Express 版本。
我已将 WebDriver.Common、WebDriver.Firefox 和 WebDriver.Remote 添加到我的解决方案中。我尝试使用这个 -
IWebElement dateOfBirth = webdriver.FindElement(By.Id("join_birth_day"));
List<IWebElement> dateOfBirthOptions = (List<IWebElement>)dateOfBirth.FindElement(By.TagName("option"));
foreach(IWebElement dateOfBirthOption in dateOfBirthOptions)
{
if (dateOfBirthOption.Equals("3"))
{
dateOfBirthOption.Select();
}
}
但在 NUnit 中运行我的解决方案时看到错误
LiveCams.CreateAccount.createAccount:
System.InvalidCastException : Unable to cast object of type 'OpenQA.Selenium.Firefox.FirefoxWebElement' to type 'System.Collections.Generic.List`1[OpenQA.Selenium.IWebElement]'.
如果我不进行转换,那么甚至无法构建解决方案。
我想我在这里错过了一些微不足道的事情。有谁可以在这里指导我吗?
在 Selenium 1.0 中,下拉选择曾经如此简单:-/
要从下拉菜单中选择一个选项,请使用以下代码
-
根据文本选择值
new SelectElement(driver.FindElement(By.XPath(""))).SelectByText("");
-
根据 Value 选择一个值
new SelectElement(driver.FindElement(By.XPath(""))).SelectByValue("");
-
根据索引选择值
new SelectElement(driver.FindElement(By.XPath(""))).SelectByIndex(0);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)