简体   繁体   English

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

[英]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. 我想我需要.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'的定义,也没有扩展方法'select'接受类型'OpenQA.Selenium.IWebDriver'的第一个参数(你是否缺少using指令或者装配参考?)

My project references include Selenium.WebDriverBackedSelenium , Thoughtworks.Selenium.Core , WebDriver , WebDriver.Support 我的项目参考包括Selenium.WebDriverBackedSeleniumThoughtworks.Selenium.CoreWebDriverWebDriver.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.

相关问题 如何使用 Selenium WebDriver C# 从下拉列表中选择一个选项? - How to select an option from drop down using Selenium WebDriver C#? 如何使用Selenium Webdriver C#从下拉列表中选择列表项的值 - How to select a list item from a drop down by its value using Selenium Webdriver C# 我无法使用Selenium C#选择下拉选项 - I cannot select an drop-down option with selenium C# 无法从 Selenium C# webdriver 的多选下拉列表(组合框)中选择值 - Not able to select value from multi select drop down (Combo box) in Selenium C# webdriver 使用硒(C#)从文本下拉菜单中选择一个项目 - Using Selenium (C#) Select an item from a drop down by text 使用C#的Selenium WebDriver:从下拉菜单中选择项(在IE中不起作用) - Selenium WebDriver with C#: select item from drop-down menu (doesn't work in IE) 如果下拉菜单不是选择元素,如何将Webdriver与C#一起使用,如何通过文本选择下拉项? - Using webdriver with c# how can I select a drop down item by text if the drop down menu is not a select element? 如何使用 C# 通过 Selenium WebDriver 获取下拉列表中的所有选项? - How to get all options in a drop-down list by Selenium WebDriver using C#? 如何使用 Selenium WebDriver 和 c# 从 Telerik RadComboBox 中选择一个选项? - How to select an option from the Telerik RadComboBox using Selenium WebDriver and c#? 如何使用Selenium Webdriver C#从弹出式下拉列表中选择一个选项 - how to select an option from the popup dropdown list using selenium webdriver c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM