问题描述
我有从下拉使用C#的webdriver结合选择价值艰难的时间。我已经在过去没有C#也没有工作的webdriver。我使用的webdriver - 硒dotnet2.0b3与Visual Studio的C#2010 Express版本。
我已经加入WebDriver.Common,WebDriver.Firefox和WebDriver.Remote我的解决方案。我试图用这个 -
IWebElement DATEOFBIRTH = webdriver.FindElement(By.Id(join_birth_day));
名单,LT; IWebElement> dateOfBirthOptions =(列表< IWebElement>)dateOfBirth.FindElement(By.TagName(选项));
的foreach(IWebElement dateOfBirthOption在dateOfBirthOptions)
{
如果(dateOfBirthOption.Equals(3))
{
dateOfBirthOption.Select();
}
}
但一定要看到错误,在运行我的解决方案时, NUnit的
LiveCams.CreateAccount.createAccount:
System.InvalidCastException:无法投型OpenQA.Selenium的'对象。 Firefox.FirefoxWebElement为键入'System.Collections.Generic.List`1 [OpenQA.Selenium.IWebElement]。
和如果我不投那么将无法甚至构建解决方案。
我想我错过了一些琐碎的事情在这里。任何一个谁可以指导我在这里?
下拉选项用于在硒1.0这么简单: - /
1)用SelectElement作为已经注释 - 中的SelectElement属于 OpenQA.Selenium.Support.UI 的命名空间。
2)你也可以做像这样用CSS选择器:
WebElement DATEOFBIRTH = webdriver.FindElement(By.Id( join_birth_day))
.FindElement(By.CssSelector(选项[值='3']))选择()。
I am having tough time in selecting value from drop down using C# binding of WebDriver. I have worked on neither C# nor WebDriver in past. I am using WebDriver - Selenium-dotnet2.0b3 with Visual Studio C# 2010 Express edition.I have added WebDriver.Common, WebDriver.Firefox and WebDriver.Remote to my solution. I tried using this -
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();
}
}
But got to see error, when running my solution in 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]'.
And if I don't cast then would not be able to even build the solution.I guess I am missing some thing trivial here. Any one who could guide me here?Drop down selection used to be so simple in Selenium 1.0 :-/
1) Using a SelectElement as already commented - How to Using Webdriver Selenium for selecting an option in C#? The SelectElement belongs to the OpenQA.Selenium.Support.UI namespace.
2) You could also do something like this with css selectors:
WebElement dateOfBirth = webdriver.FindElement(By.Id("join_birth_day"))
.FindElement(By.CssSelector("option[value='3']")).Select();
这篇关于的webdriver + C#+选择下拉值下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!