How To Handle Drop Down in WebDriver

Handling Drop Down in WebDriver seems so tedious but WebDriver API has gifted us with one class Select and  this is one good option to replace WebElement to handle Drop -Down and some time it also works for List also.

So let start with small part of HTML code

<select name="height">
<option value="3">3ft</option>
<option value="4">4ft</option>
<option value="5">5ft</option>
<option value="6">6ft</option>
</select>

3ft4ft5ft6ftBut always remember this works only on Select tag that is used for creating Drop down and list.
Select Class in WebDriver has many methods and function that interacts with Drop Down.
so let see how we should handle drop down

@Test
	public void test()
	{
	driver.get("C:/Users/kaushal/Desktop/Size_DropDown.htm");
	/*to get options of drop down we would use name=height attribute that is given in above code

	*/
	Select select = new Select(driver.findElement(By.name("height")));
	//finding the number of option using size() function
	int i =select.getOptions().size();
	//printing the size
	System.out.print("number of options ="+i);
	//verifying number of option is 4
	assertEquals(4, select.getOptions().size());
	// selecting option in Drop-down using Visible Text
	select.selectByVisibleText("5ft");
	assertEquals("5ft", select.getFirstSelectedOption().getText());

	//selecting option in Drop-down using value attribute
	select.selectByValue("4");
	assertEquals("4ft", select.getFirstSelectedOption().getText());

	//selecting option in Drop-down using value attribute
	select.selectByIndex(1);
	assertEquals("4ft", select.getFirstSelectedOption().getText());

	/*verifying the options of drop down first we would put all value in one list and then we would add all value fetched from instance of Select Class that we have used before in code in another array list and then we would verify the value*/
	//first string type list that would contain all option value
	List list = Arrays.asList(new String[]{"3ft",
	"4ft", "5ft","6ft"});
	List  list1 = new ArrayList();
	//adding all option captured by select in to list1 arraylist
	for(WebElement element:select.getOptions())
	{
	list1.add(element.getText());
	}
	//to see what is being added to arraylist use this code
	int j = list1.size();
	System.out.println("value entered in J ="+j);
	for(int k = 1; k<j; k++)
	{
	System.out.println(list1.get(k));
	}
	Assert.assertArrayEquals(list.toArray(), list1.toArray());

	}

I have used this code and where working fine..to execute this code I have used Junit framework and would let you know once I would performa the same with Testng framework

10 thoughts on “How To Handle Drop Down in WebDriver

  1. Hi Dwarika,

    Thanks for the post but while i was using above example , i am getting an error message as “The method assertEquals(int, int) is undefined for the type GoogleSuggest” where GoogleSuggest is the name of the class.
    Can you please suggest something on this?

  2. Hi,
    I’m getting an error message as”The method assertEquals(int, int) is undefined for the type DropDown” where DropDown is the name of the Class….
    I’m waiting for your valuable suggestions???????

  3. This assertion error so I would request that just count the number of drop- down manual and then use

    assertEquals(“your value that you have counted”, select.getOptions().size());

    Here

    select.getOptions().size() gives the number of options available in dropdown.

    Steps to handle dropdown
    1- Move to drop-down
    2- Right click and inspect the element without selecting any option from the drop-down
    3- Now find the xpath either by yourself or just do right click and click on Copy Xpath
    4- put it in to

    Select select = new Select(driver.findElement(By.xpath(“you xpath”))
    5- Now find the number of option by using
    select.getOptions.size(); and compare this with actual number

    Hope this will make it clear to understand the handling of drop down.

  4. Use the as usual sendkeys method to enter phone number like -

    driver.findElement(By.id(“ctl00_mainbody_txtPhone1″)).sendkeys(“”1234567890);

    Try if it works!

  5. Hello,

    Please i need help i’m new in seleniumwebdriver!!!
    How can i handle this situacion in a dropdown with ul class:

    this is my code:
    driver.findElement(By.xpath(OR.getProperty(“//*[@id='search-filter-facet-categoryId']/div/div/div”))).sendKeys(listcatg);

    listcatg is take it from doc.xls. where listcatg = Prendas superiores

    Categoría
    Prendas superiores
    Prendas inferiores

    Code:

    Categoría

    Prendas superiores

    Prendas inferiores

    thank so much
    Kriss

      • CODE of url>
        ul class=”facet-fields”
        li
        div id=”search-filter-facet-categoryId” class=”search-filter search-filter-list active” data-input-value=”” data-input-type=”standard” data-input-name=”categoryId”
        div class=”custom-select-list”
        div class=”search-filter-content”
        div class=”search-filter-wrapper”
        span class=”search-filter-label” data-property=”GLB_CATEGORY”>Categoría
        Prendas superiores

  6. Hi..
    I want to konw how to handle drop down which contains arrow button. After clicking on arrow button again one list comes out and I want to select one option amonga them.
    Can you please give me solution for this ASAP.

    Thanks and Regards
    Sourabh Chanchawat

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>