微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

这个问题在这里已经有一个答案:> 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();

原文地址:https://www.jb51.cc/csharp/95255.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐