Advanced Selenium Concepts – Custom Select Class – Select Value Containing Specific Text in Drop down

Hello Folks,

Most of us we know that to handle a dropdown developed using Select tag, we can use inbuilt class of Selenium named “Select” class. This class provides you three methods to select options in dropdown which are below:

selectByValue – You need to provide exact value to get it selected.
selectByVisibleText – You need to provide exact text to get it selected.
selectByIndex – You must need to know index of desired of desired value.

If you want to select any value which contains some specific text, it will not be easy with above methods. I see people create their own special methods to handle this scenarios. But instead of using other ways, we can modify inbuilt functionality of class “Select” itself and use it for our specific scenarios.  So, in this post, I will explain how can we create a new method to select a option containing specific text.

Let’s create a select tag drop down as below:

Select your skill below:

Scenario : Select option which contains “Testing” in drop down.

We know it is not possible using inbuilt methods selectByVisibleText and selectByValue of Select class and selectByIndex will work only if you know the index in advance. Index is not a good option as it will change if new option is added in drop down.

So we will create a new custom method for Select class. I will use concept implemented in actual Select class with modification as required.

package PageObjectModelUsingPageFactory;

import java.util.Iterator;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Quotes;
import org.openqa.selenium.support.ui.UnexpectedTagNameException;

public class CustomSelect {
	
	
	private final WebElement element;
	private final boolean isMulti;

	public CustomSelect(WebElement element) {
		String tagName = element.getTagName();
		if (null != tagName && "select".equals(tagName.toLowerCase())) {
			this.element = element;
			String value = element.getAttribute("multiple");
			this.isMulti = value != null && !"false".equals(value);
		} else {
			throw new UnexpectedTagNameException("select", tagName);
		}
	}

	public boolean isMultiple() {
		return this.isMulti;
	}




	private void setSelected(WebElement option, boolean select) {
		boolean isSelected = option.isSelected();
		if (!isSelected && select || isSelected && !select) {
			option.click();
		}

	}

	// Method to select option with containing text
	public void selectByPartialVisibleText(String text) {
		List options = this.element
				.findElements(By.xpath(".//option[contains(normalize-space(.) , " + Quotes.escape(text) + ")]"));
		boolean matched = false;

		for (Iterator subStringWithoutSpace = options.iterator(); subStringWithoutSpace.hasNext(); matched = true) {
			WebElement candidates = (WebElement) subStringWithoutSpace.next();
			this.setSelected(candidates, true);
			if (!this.isMultiple()) {
				return;
			}
		}
		
		if (!matched) {
			throw new NoSuchElementException("Cannot locate element with text: " + text);
		}

	}

}

Scenario java code:

package HandlingDropdown;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import PageObjectModelUsingPageFactory.CustomSelect;

public class SingleSelectDropdown {

	public static void main(String[] args) throws InterruptedException {
		System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver.exe");
		WebDriver driver= new ChromeDriver();
		driver.get("file:///C:/Users/amodm/Downloads/CustoSelect.html");
		
		// Locating select tag web element
		WebElement singleSelectTagDropdownWebElement= driver.findElement(By.id("SingleDD"));
		
		// Custom select
			
		CustomSelect selectObject= new CustomSelect(singleSelectTagDropdownWebElement);
		selectObject.selectByPartialVisibleText("Testing");
		
	}
}

Output:

We will see some more custom methods in upcoming posts.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

#ThanksForReading

#HappySelenium

3 thoughts on “Advanced Selenium Concepts – Custom Select Class – Select Value Containing Specific Text in Drop down”

  1. how to wait for ASP page to postback after selecting a dropdown list.

    I want to select a dependent dropdownlist in asp website which gives pincode of city after selecting State-District-City as dependent dropdown.
    please help me on this.

    please mail to my email if you got answer.
    i’m waiting…

Leave a Reply

Your email address will not be published. Required fields are marked *