Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/21/2025 By admin

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:
Select
Java
Automation Testing
API
Hadoop
BigData
Devops

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

Uncategorized

Post navigation

Previous Post: Part 5: Usages Of Javascripts In Selenium: Understanding Method executeAsyncScript Of JavascriptExecutor
Next Post: How to launch Chrome browser as headless in Selenium Java

Related Posts

July 17, 2017 – Make Selenium Easy Uncategorized
RetryResult – Make Selenium Easy Uncategorized
December 23, 2018 – Make Selenium Easy Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
TestNG Tutorials 69 : Rerun Failed Test Method Using IRetryAnalyzer Interface – Implementing With Listener – IAnnotationTransformer Uncategorized
TestNG Tutorials 29: Grouping Configuration Methods in TestNG Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark