Select Option in a Select using Partial Text

If you ever come across the need to select an option in a drop down list when you only have some of the text here is how you can select a particular option oin a drop down list using a partial text selector. In this example the drop down list would be populated with strings formatted as follows: FirstName LastName – Address. eg “John Smith – 1001 Main St”. For the test we only have the person’s name available to us.

IWebElement ddlList = driver.FindElement(By.Id("ddlList"));
string strName = "John Smith";

ddlList.FindElement(By.XPath(string.Format("./option[starts-with(text(), '{0}')]", strName))).Click();

Leave a Comment