使用 Selenium WebDriver C# 在下拉列表中选择每个选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15535069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:05:37  来源:igfitidea点击:

Select each option in a drop down using Selenium WebDriver C#

c#selenium

提问by Ben Walker

I'm not able to select options in a drop down list. I think I need to have .Selector SelectElement, but there is no such option.

我无法在下拉列表中选择选项。我想我需要有.SelectSelectElement,但没有这样的选择。

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”的定义,并且找不到接受“OpenQA.Selenium.IWebDriver”类型的第一个参数的扩展方法“select”(您是否缺少 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;

采纳答案by Nashibukasan

Depending what version of Selenium WebDriver you are using you can use the SelectElementclass, which will be included in OpenQA.Selenium.Support.UI.
For example:

根据您使用的 Selenium WebDriver 版本,您可以使用SelectElement该类,该类将包含在OpenQA.Selenium.Support.UI.
例如:

SelectElement selector = new SelectElement(element);
selector.SelectByIndex(1);

Where the elementis your drop down box.

元素是你的下拉框。

回答by CheryJose

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"

Step 1. 创建 web element 标签的接口 Step 2. 使用 web element tag 的所有子元素创建一个 IList Step 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();
        }
    }
}

回答by Madhu

You can also use:

您还可以使用:

new SelectElement(driver.FindElement(By.Id("")).SelectByText(""));

Or:

或者:

new SelectElement(driver.FindElement(By.Id("")).SelectByValue(""));

回答by Arnab

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());

回答by Mninawe Richard Matrose

This works perfectly ...

这完美地工作......

SelectElement selector = new SelectElement(element);
selector.SelectByIndex(1);

Where the element is your drop down box.

元素是您的下拉框。