【问题标题】:Select a value from drop down using Selenium WebDriver C# [duplicate]使用 Selenium WebDriver C# 从下拉列表中选择一个值 [重复]
【发布时间】:2011-07-25 04:45:34
【问题描述】:

我很难使用 WebDriver 的 C# 绑定从下拉列表中选择值。我过去既没有研究过 C#,也没有研究过 WebDriver。我正在使用 WebDriver - Selenium-dotnet2.0b3 和 Visual Studio C# 2010 Express 版本。 我已将 WebDriver.Common、WebDriver.Firefox 和 WebDriver.Remote 添加到我的解决方案中。我试过用这个 -

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

但是在 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]'.

如果我不投,那么甚至无法构建解决方案。 我想我在这里遗漏了一些琐碎的事情。有谁能在这里指导我吗? 在 Selenium 1.0 中,下拉选择曾经非常简单:-/

【问题讨论】:

标签: c# webdriver selenium-webdriver


【解决方案1】:

要从下拉菜单中选择一个选项,请使用以下代码

  1. 根据文本选择值

    new SelectElement(driver.FindElement(By.XPath(""))).SelectByText("");
    
  2. 根据值选择值

    new SelectElement(driver.FindElement(By.XPath(""))).SelectByValue("");
    
  3. 根据索引选择值

    new SelectElement(driver.FindElement(By.XPath(""))).SelectByIndex(0);
    

【讨论】:

  • 这里不要吹毛求疵,但是对于那些可能会感到困惑的人,在按索引选择时,您不需要在SelectByIndex()中使用引号,只需一个int。
  • 这一切现在都在documentation
【解决方案2】:

1) 使用已注释的 SelectElement - How to select an option from drop down using Selenium WebDriver C#? SelectElement 属于 OpenQA.Selenium.Support.UI 命名空间。

2) 你也可以用 css 选择器做这样的事情:

WebElement dateOfBirth =  webdriver.FindElement(By.Id("join_birth_day"))
                              .FindElement(By.CssSelector("option[value='3']")).Select();

【讨论】:

  • 我无法获得 SelectElement 的引用,我错过了任何 DLL 吗?
  • 它实际上是 OpenQA.Selenium.Support.UI.SelectElement 的一部分。我已经更新了答案。是的,请仔细检查您是否已导入所有 dll。
  • 现在知道了,谢谢。我将坚持使用 CSS 选择
  • 真的这个问题应该被标记为重复,而不是在另一个答案中重复你的答案
  • 这对我来说根本不起作用。 '方法'Select'没有重载需要0个参数。'
【解决方案3】:
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://YourDDListpageURL.html");
            IWebElement element = driver.FindElement(By.XPath("//Select"));
            //You can locate the element by using the ID / Name as well IList
            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();
        }
    }
}

【讨论】:

  • edit 回答正确的代码格式。
【解决方案4】:

使用在 OpenQA.Selenium.Support.UI 命名空间中定义的以下类 SelectElement,单词 Select 已在 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多