Selenium-如何检查列表框中是否存在文本

时间:2016-04-12 10:45:16

标签: c# selenium listbox

我正在尝试验证列表框中添加的文本是否已成功删除。使用C#在Selenium中处理此类场景的最佳方法是什么?

以下是我目前使用的代码。

//Verify that the subject is added and then deleted
    public static void VerifySubjectDel()
    {
        string subjectAddValue = GenerateRandomAlphaCode(200);
        productPage.subjectAddTxtBx.SendKeys(subjectAddValue);
        productPage.subjectAddBtn.Click();

        IWebElement elem = WebDriver.FindElement(By.Id("Subjects_ListBox"));
        SelectElement selectList = new SelectElement(elem);
        IList<IWebElement> options = selectList.Options;

        if (options.ToList().Any(tagname => tagname.Text.Contains(subjectAddValue)))
        {
            Assert.IsTrue(true);
            selectList.SelectByText(subjectAddValue);
            productPage.subjectDelBtn.Click();
            WebDriver.SwitchTo().Alert().Accept();
            bool subjectDel = WebDriver.FindElements(By.XPath(".//*[@id='Subjects_ListBox']//option[contains(text(),'" + subjectAddValue + "')]")).Count == 0;
            if (subjectDel)
            {
                Assert.IsTrue(subjectDel);
            }
            else
                Assert.IsTrue(subjectDel, "Subject not deleted successfully");
        }
        else
            Assert.IsTrue(false, "The Subject added is not present in the Subject-ListBox");
    }

1 个答案:

答案 0 :(得分:0)

我会在您上面捕获的IWebElement上调用FindElements并返回列表框中的所有元素。然后使用LINQ,您可以执行类似

的操作
bool success = !listBoxItems.Any(x => string.Compare(x.Text, subjectAddValue) == 0):