使用SelectByText(部分)与C#Selenium WebDriver绑定似乎不起作用

Ali*_*ott 6 .net c# selenium webdriver selenium-webdriver

我在C#中使用Selenium WebDriver Extensions通过部分文本值从选择列表中选择一个值(实际前面有一个空格).我无法使用部分文本匹配来使其工作.我做错了什么或这是一个错误?

可重复的例子:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace AutomatedTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://code.google.com/p/selenium/downloads/list");
            var selectList = new SelectElement(driver.FindElement(By.Id("can")));
            selectList.SelectByText("Featured downloads");
            Assert.AreEqual(" Featured downloads", selectList.SelectedOption.Text);
            selectList.SelectByValue("4");
            Assert.AreEqual("Deprecated downloads", selectList.SelectedOption.Text);
            driver.Quit();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

提供错误: OpenQA.Selenium.NoSuchElementException: Cannot locate element with text: Featured downloads

Ali*_*ott 8

SelectByText方法是坏了,所以我写了叫我自己的扩展方法SelectBySubText做的这是什么意思做.

using System.Linq;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

namespace AutomatedTests.Extensions
{
    public static class WebElementExtensions
    {
        public static void SelectBySubText(this SelectElement me, string subText)
        {
            foreach (var option in me.Options.Where(option => option.Text.Contains(subText)))
            {
                option.Click();
                return;
            }
            me.SelectByText(subText);
        }
    }
Run Code Online (Sandbox Code Playgroud)