如何点击selenium webdriver c#中突出显示的选项?

时间:2015-11-16 12:28:01

标签: c# selenium selenium-webdriver webdriver

我正在使用select2元素。我通过单击箭头打开选项栏,然后输入文本以将选项过滤为仅一个,然后我不知道如何点击该选项,因此可以选择任何想法​​吗?

    driver.FindElement(By.XPath("//*[@id='s2id_testid']/a/span[2]/b")).Click();
    driver.FindElement(By.CssSelector("#select2-drop input.select2-input")).SendKeys("54");

1 个答案:

答案 0 :(得分:1)

您可以像使用FindElement功能一样利用驱动程序的强大功能。不要使用By.XPathBy.CssSelector使用By.Id,而只需确保您尝试点击的select2元素option具有ID。如果没有,请确保在标记中分配了一个,或者在呈现HTML时生成可预测的。

    driver.FindElement(By.Id("myKnownId")).Click();

我经常尝试使用selenium功能作为可扩展性的手段。例如,您可以使用以下内容检查具有给定标识符的元素是可见还是不可见。

    [Then(@"An element with the identifier (.*) is (visible|not visible)")]
    public void ThenAnElementWithTheIdentifierIsVisible(string identifier, string visibility)
    {
        if (visibility.Equals("visible"))
        {
            Debug.WriteLine(string.Format("Starting to wait for element with identifier {0} to be visible.", identifier));
            Driver.WaitUntil(d => d.FindElement(By.Id(identifier))
                                   .Displayed);

        }
        else if (visibility.Equals("not visible"))
        {
            Debug.WriteLine(string.Format("Starting to wait for element with identifier {0} to not be visible.", identifier));
            Driver.WaitUntil(d => !d.FindElement(By.Id(identifier))
                                    .Displayed);
        }
    }

这允许重复使用,您可以轻松地看到在构建UI测试时这会变得多么强大。

相关问题