[英]Select each option in a drop down using Selenium WebDriver C#
I'm not able to select options in a drop down list. 我无法在下拉列表中选择选项。 I think I need to have .Select
or SelectElement
, but there is no such option. 我想我需要.Select
或SelectElement
,但没有这样的选择。
Sample code: 示例代码:
IWebDriver ffbrowser = new FirefoxDriver();
ffbrowser.Navigate().GoToUrl("http://www.amazon.com/");
ffbrowser.Manage().Window.Maximize();
Thread.Sleep(500);
IWebElement ddl = ffbrowser.FindElement(By.Name("url"));
int numofitems = ddl.FindElements(By.TagName("option")).Count;
for (int i = 1; i < numofitems; i++)
{
ffbrowser.select("TagName = option", "index = i");
}
The "select" in "ffbrowser.select" is reported as an error: “ffbrowser.select”中的“select”报告为错误:
Error 1 'OpenQA.Selenium.IWebDriver' does not contain a definition for 'select' and no extension method 'select' accepting a first argument of type 'OpenQA.Selenium.IWebDriver' could be found (are you missing a using directive or an assembly reference?) 错误1'OpenQA.Selenium.IWebDriver'不包含'select'的定义,也没有扩展方法'select'接受类型'OpenQA.Selenium.IWebDriver'的第一个参数(你是否缺少using指令或者装配参考?)
My project references include Selenium.WebDriverBackedSelenium
, Thoughtworks.Selenium.Core
, WebDriver
, WebDriver.Support
我的项目参考包括Selenium.WebDriverBackedSelenium
, Thoughtworks.Selenium.Core
, WebDriver
, WebDriver.Support
and I have 我有
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
Depending what version of Selenium WebDriver you are using you can use the SelectElement
class, which will be included in OpenQA.Selenium.Support.UI
. 根据您使用的Selenium WebDriver的版本,您可以使用SelectElement
类,该类将包含在OpenQA.Selenium.Support.UI
。
For example: 例如:
SelectElement selector = new SelectElement(element);
selector.SelectByIndex(1);
Where the element is your drop down box. 元素是你的下拉框。
Here is an example to better illustrate how to get all the items in a drop down list and to select an item from the drop down list. 下面是一个示例,可以更好地说明如何获取下拉列表中的所有项目以及从下拉列表中选择项目。
A sample Html code for drop down list 下拉列表的示例Html代码
<select>
<option>Milk</option>
<option>Coffee</option>
<option>Tea</option>
</select>
Code below gets all the items from the drop down list above and selects item 'Coffee'.Logic of the code is as follows 下面的代码从上面的下拉列表中获取所有项目,并选择项目'Coffee'。代码的逻辑如下
Step 1. Create an interface of the web element tag Step 2. Create an IList with all the child elements of web element tag Step 3. Select the Drop List item "Coffee" 步骤1.创建web元素标记的界面步骤2.使用web元素标记的所有子元素创建IList步骤3.选择Drop List项“Coffee”
using System;
using System.Collections.Generic;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace SeleniumTests
{
class DropDownListSelection
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://DropDownList.html");
IWebElement element = driver.FindElement(By.XPath("//Select"));
IList<IWebElement> AllDropDownList = element.FindElements(By.XPath("//option"));
int DpListCount = AllDropDownList.Count;
for (int i = 0; i < DpListCount; i++)
{
if (AllDropDownList[i].Text == "Coffee")
{
AllDropDownList[i].Click();
}
}
Console.WriteLine(DpListCount);
Console.ReadLine();
}
}
}
You can also use: 您还可以使用:
new SelectElement(driver.FindElement(By.Id("")).SelectByText(""));
Or: 要么:
new SelectElement(driver.FindElement(By.Id("")).SelectByValue(""));
Use the below simple sample code: 使用以下简单的示例代码:
String Input="Value to Select";
String xPathVal="@["id=Samplexpath"]";
IWebElement TargetElement = driver.FindElement(By.XPath(xPathVal));
SelectElement dropdown = new SelectElement(TargetElement);
dropdown.SelectByText(Input.Trim());
This works perfectly ... 这很完美......
SelectElement selector = new SelectElement(element);
selector.SelectByIndex(1);
Where the element is your drop down box. 元素是你的下拉框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.