C# 使用webdriver从下拉列表中查找元素
我正在编写一个简单的自动化测试应用程序,我已经到了必须为该应用程序编写n个单元测试的地步 我的问题是,我想用系统中的默认值断言用户选择的值,有人能帮我吗 这是我的代码:C# 使用webdriver从下拉列表中查找元素,c#,selenium-webdriver,C#,Selenium Webdriver,我正在编写一个简单的自动化测试应用程序,我已经到了必须为该应用程序编写n个单元测试的地步 我的问题是,我想用系统中的默认值断言用户选择的值,有人能帮我吗 这是我的代码: IWebElement dropDownListBox = _driver.FindElement(By.Id("ddlWorkflow")); dropDownListBox.Click(); Assert.AreEqual(-1, string.Compare("CD-A", dropDownListBox.Text, tr
IWebElement dropDownListBox = _driver.FindElement(By.Id("ddlWorkflow"));
dropDownListBox.Click();
Assert.AreEqual(-1, string.Compare("CD-A", dropDownListBox.Text, true));
Selenium中的下拉菜单称为
Select Element
。为了从列表中选择项目,我们需要使用Selenium的SelectElement
类,它是WebDriver.Support.dll
的一部分。请尝试下面的代码
SelectElement selectElement = new SelectElement(_driver.FindElement(By.Id("ddlWorkflow")));
selectElement.SelectByText(dropdown item text); // Or can use Index
selectElement.SelectByIndex(dropdown item index);
要获取下拉项所选文本,需要使用以下选项
selectElement.SelectedOption.Text;
最后的代码是这样的
Assert.AreEqual("CD-A", selectElement.SelectedOption.Text, "Dropdown item should be selected.");
如果这对你有用,一定要告诉我