What are the different ways to select an option from a dropdown using Selenium Webdriver?

0 votes
What are the different ways to select an option from a dropdown using Selenium Webdriver?
Jul 5, 2019 in Selenium by Swathi
4,900 views

1 answer to this question.

0 votes

Hi Swathi, there are a few ways by which you can select an Option from a Dropdown on Webpage using Selenium. Following are a few:

  1. selectByValue(): You can select an option by using Value attribute provided for each option in dropdown menu. So you can use this Value to select any particular option:
    • WebElement element = driver.findElement(By.id("year")); 
      Select select = new Select(element); 
      select.selectByValue("4"); // This command will select the year with value 4.
  2. selectByIndex(): Here you can use the index to select the option from a drop down. Basically Index starts from 0, so  this means for the first option, index will be 0 and so on:
    • WebElement element = driver.findElement(By.id("year")); 
      Select select = new Select(element); 
      select.selectByIndex("4"); // This command will select the year with index 4 i.e. 5th option
  3. selectByVisibleText() : You can also use the visible text to select the option. So if you wants to select “2015”, you can select the option by visible text i.e. 2015:

    • WebElement element = driver.findElement(By.id("year")); 
      Select select = new Select(element); 
      select.selectByVisibleText("2015");
answered Jul 6, 2019 by Anvi
• 14,050 points

Related Questions In Selenium

0 votes
1 answer

Select an item from a dropdown list using Selenium WebDriver

Use this then it will work - new ...READ MORE

answered Apr 9, 2018 in Selenium by Vardy
• 2,370 points
3,986 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

What is the best way to handle a Javascript popup using Selenium Webdriver?

Hello Dimple, the simplest way to handle ...READ MORE

answered May 10, 2019 in Selenium by Pratibha
• 3,690 points
2,121 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
2,587 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,870 points
2,041 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,870 points
2,138 views
0 votes
1 answer

How can I select an option from a dropdown using Ruby Selenium Webdriver?

Hey Swasti, you can try following lines ...READ MORE

answered Aug 26, 2019 in Selenium by Anvi
• 14,050 points
579 views
0 votes
1 answer

How can I refresh a browser window in different ways using Selenium Webdriver?

Hello Piyush, you can refresh a browser ...READ MORE

answered May 28, 2019 in Selenium by Anvi
• 14,050 points
533 views