C# 使用Selenium WebDriver C从下拉列表中选择一个值
我在使用WebDriver的C绑定从下拉列表中选择值时遇到了困难。我过去既没有使用过C语言,也没有使用过WebDriver。我正在将WebDriver-Selenium-dotnet2.0b3与Visual Studio C 2010 Express edition配合使用。 我已将WebDriver.Common、WebDriver.Firefox和WebDriver.Remote添加到我的解决方案中。我试过用这个-C# 使用Selenium WebDriver C从下拉列表中选择一个值,c#,webdriver,selenium-webdriver,C#,Webdriver,Selenium Webdriver,我在使用WebDriver的C绑定从下拉列表中选择值时遇到了困难。我过去既没有使用过C语言,也没有使用过WebDriver。我正在将WebDriver-Selenium-dotnet2.0b3与Visual Studio C 2010 Express edition配合使用。 我已将WebDriver.Common、WebDriver.Firefox和WebDriver.Remote添加到我的解决方案中。我试过用这个- IWebElement dateOfBirth = webdriver.Fi
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();
}
}
如果我不进行强制转换,那么我甚至无法构建解决方案。
我想我错过了一些琐碎的事情。有人能带我来这里吗?
在Selenium 1.0中,下拉选择过去非常简单:-/1使用已注释的SelectElement-SelectElement属于OpenQA.Selenium.Support.UI命名空间 2您也可以使用css选择器执行类似操作: WebElement dateOfBirth=webdriver.FindElementBy.Idjoin\u birth\u day .FindElementBy.cssselector选项[value='3'].Select;
使用OpenQA.Selenium.Support.UI命名空间中定义的以下类SelectElement选择一词已在C中使用,这就是为什么它的实现发生了更改,并且类的名称也不同
// Summary:
// Initializes a new instance of the SelectElement class.
//
// Parameters: element - The element to be wrapped
//
public SelectElement(IWebElement element);
创建这个类的一个对象,可以根据索引、文本和值进行选择
// Summary:
// Select the option by the index, as determined by the "index" attribute of
// the element.
//
// Parameters:
// index:
// The value of the index attribute of the option to be selected.
public void SelectByIndex(int index);
// Summary:
// Select all options by the text displayed.
//
// Parameters:
// text:
// The text of the option to be selected. If an exact match is not found, this
// method will perform a substring match.
// Remarks:
// When given "Bar" this method would select an option like:
// <option value="foo">Bar</option>
public void SelectByText(string text);
// Summary:
// Select an option by the value.
//
// Parameters:
// value:
// The value of the option to be selected.
// Remarks:
// When given "foo" this method will select an option like:
// <option value="foo">Bar</option>
public void SelectByValue(string value);
要从下拉列表中选择选项,请使用以下代码 基于文本选择值的步骤
new SelectElement(driver.FindElement(By.XPath(""))).SelectByText("");
根据值选择值的步骤
new SelectElement(driver.FindElement(By.XPath(""))).SelectByValue("");
根据索引选择值的步骤
new SelectElement(driver.FindElement(By.XPath(""))).SelectByIndex(0);
这有用吗?没有,我想知道哪个名称空间有类-SelectElement我无法获得SelectElement的引用,我是否错过了任何DLL?它实际上是OpenQA.Selenium.Support.UI.SelectElement的一部分。我已经更新了答案。是的,仔细检查你已经导入了所有的dll。现在知道了,谢谢。而且我会坚持css选择。真的,这个问题应该被标记为重复,而不是在另一个答案中重复你的答案。这对我根本不起作用方法“Select”的无重载包含0个参数。请回答正确的代码格式。这里不要吹毛求疵,但对于那些可能会混淆的问题,在按索引选择时,您不需要在SelectByIndex中加引号,只需要一个int。这一部分现在已全部介绍
new SelectElement(driver.FindElement(By.XPath(""))).SelectByValue("");
new SelectElement(driver.FindElement(By.XPath(""))).SelectByIndex(0);