使用 Selenium WebDriver C# 从下拉列表中选择一个值

发布于 2024-10-27 19:54:47 字数 974 浏览 0 评论 0原文

我在使用 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 中,下拉选择曾经如此简单:-/

I am having tough time in selecting value from drop down using C# binding of WebDriver. I have worked on neither C# nor WebDriver in past. I am using WebDriver - Selenium-dotnet2.0b3 with Visual Studio C# 2010 Express edition.
I have added WebDriver.Common, WebDriver.Firefox and WebDriver.Remote to my solution. I tried using this -

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

But got to see error, when running my solution in 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]'.

And if I don't cast then would not be able to even build the solution.
I guess I am missing some thing trivial here. Any one who could guide me here?
Drop down selection used to be so simple in Selenium 1.0 :-/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长安忆 2024-11-03 19:54:47

要从下拉列表中选择选项,请使用以下代码

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

To select an Option from Drop Down use the below code

  1. To select a value based on Text

    new SelectElement(driver.FindElement(By.XPath(""))).SelectByText("");
    
  2. To select a value based on Value

    new SelectElement(driver.FindElement(By.XPath(""))).SelectByValue("");
    
  3. To select a value based on Index

    new SelectElement(driver.FindElement(By.XPath(""))).SelectByIndex(0);
    
幽蝶幻影 2024-11-03 19:54:47

1)使用已经评论过的 SelectElement - 如何使用 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();

1) Using a SelectElement as already commented - How to select an option from drop down using Selenium WebDriver C#? The SelectElement belongs to the OpenQA.Selenium.Support.UI namespace.

2) You could also do something like this with css selectors:

WebElement dateOfBirth =  webdriver.FindElement(By.Id("join_birth_day"))
                              .FindElement(By.CssSelector("option[value='3']")).Select();
安穩 2024-11-03 19:54:47

使用 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);

Use the Following Class SelectElement defined in OpenQA.Selenium.Support.UI namespace the word Select is already used in C# that is why its implementation is changed and class is named differently.

    // Summary:
    //     Initializes a new instance of the SelectElement class.
    //
    // Parameters: element - The element to be wrapped
    //
    public SelectElement(IWebElement element);

Create an object of this class and there is option of selection based on index, text and value.

    // 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);
撞了怀 2024-11-03 19:54:47
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();
        }
    }
}
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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文