Using SelectByText (partial) with C# Selenium WebDriver bindings doesn't seem to work

.net, c#, selenium, selenium-webdriver, webdriver

Solution

The SelectByText method is broken, so I wrote my own extension method called SelectBySubText to do what it is meant to do.

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);
        }
    }

Problem

I am using the Selenium WebDriver Extensions in C# to select a value from a select list by a partial text value (the actual has a space in front). I can't get it to work using a partial text match. Am I doing something wrong or is this a bug? Reproducible example: ``` 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(); } } } ``` Provides error: `OpenQA.Selenium.NoSuchElementException: Cannot locate element with text: Featured downloads`

Original source