How To Select Option By Value or By Index In Selenium WebDriver With Example

In my earlier post, We have seen example of HOW TO SELECT OPTION BY VISIBLE TEXT from drop down. There are two more alternatives to select option value from drop down or list box in selenium webdriver software testing tool. 1. Selecting Option By Value and 2. Selecting Option By Index. You can use any one from these three to select value from list box of your software web application page. You can use select by index in selenium when you are not able to select is value or visible text. you can use selectbyindex selenium method to achieve it. If you know, we can use "select" command or "addSelection" command in selenium IDE software testing tool to select option from list box.

First of all let me show you difference between visible text, value and index of list box option. Bellow given image will show you clear difference between value, visible text and index.

1. Select Option By Value In WebDriver

Syntax for selecting option by value in webdriver is as bellow. First syntax will locate the select element from page of software web application and 2nd syntax will select the option from select box by value = Italy.
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
listbox.selectByValue("Italy");

2. Select Option By Index In WebDriver

Syntax for select by index in selenium from list box is as bellow. It will select 1st element(index = 0) from select box of software web application page using selectbyindex selenium method..
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
listbox.selectByIndex(0);

Execute bellow given example in your eclipse and verify result. Try to use it in different applications.

Copy bellow given @Test method part of select option by value or index and replace it with the @Test method part of example given on THIS PAGE.(Note : @Test method is marked with pink color in that linked page).

selectbyindex selenium and selectbyvalue selenium example

@Test
 public void test () throws InterruptedException 
 {  
  driver.findElement(By.id("text1")).sendKeys("My First Name");

  //Selecting value from drop down by value
  Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
  listbox.selectByValue("Italy");
  listbox.selectByValue("Mexico");
  listbox.selectByValue("Spain");

  driver.findElement(By.xpath("//input[@value='->']")).click();
  WebDriverWait wait = new WebDriverWait(driver, 15);
  wait.until(ExpectedConditions.elementToBeClickable(By.id("text2")));

//Selecting value from drop down by index
  listbox.selectByIndex(0);
  listbox.selectByIndex(3);
  driver.findElement(By.xpath("//input[@value='->']")).click();
  Thread.sleep(2000);
 }
This way selectbyindex selenium and selectbyvalue selenium methods works.

6 comments:

  1. how to select element from bootsrap dropdown??
    my code is


    please specify the code , how to click on chosen container first? and how to click on li element.
    my code is


    div id="category_chosen" class="chosen-container chosen-container-single" style="width: 228px;" title=""
    a class="chosen-single chosen-default" tabindex="-1"
    span Select an Option span
    div
    bb
    /div
    a
    div class="chosen-drop"
    div class="chosen-search"
    input type="text" autocomplete="off"
    div
    ul class="chosen-results"
    li class="active-result" style="" data-option-array-index="1"Europe/li
    li class="active-result" style="" data-option-array-index="2"Mahindra Europe S.R.L.li
    li class="active-result" style="" data-option-array-index="3"Mahindra Heavy Engines Pvt.\

    i have removed tag signs<>

    ReplyDelete
  2. How to get all values of the dropdown and store them in a list ?

    ReplyDelete
    Replies
    1. List drplist= driver.findElements(By.id("Shirts"));
      int size=drplist.size();
      for(int i=0; i<size; i++)
      {

      System.out.println(drplist.get(i).getText());
      }

      Delete
  3. Please help me i spent whole day on selecting value from drop down i tried almost every method mentioned here ,get by id ,by value ,by index .

    ReplyDelete
    Replies
    1. WebElement Shirts=driver.findElement(By.id("Shirts"));
      Select shirtelement=new Select(Shirts);
      shirtelement.selectByVisibleText("Yellow Shirt");

      Delete
  4. HOW TO USE SELECT METHOD WHILE USING API S
    HOW TO MATCH DROP DOWN DATA WITH THE DATA IN EXCEL SHEET

    ReplyDelete