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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s