加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:33:48 所属栏目:百科 来源:网络整理
导读:这个问题在这里已经有一个答案: How to select an option from drop down using Selenium WebDriver C#?8 使用WebDriver的C#绑定,我很难从下拉列表中选择值.我以前在C#和WebDriver上工作过.我正在使用WebDriver – Selenium-dotnet2.0b3与Visual Studio C#2
这个问题在这里已经有一个答案:> How to select an option from drop down using Selenium WebDriver C#?8
使用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中下拉选择过于简单: – /

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读