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
24,955 views

3 answers 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,150 points
can you explain me the 3rd option deeply what are the webElement element and y we need to call the class or else this is the format for the dropdown function

it  is backage or class and y have any path clear explanation.

guide me any video for this example

selectByVisibleText all options that display text matching the given argument:

java:

WebElement element = driver.findElement(By.name("Countries"));
Select s = new Select(element);
s.selectByVisibleText("Brasil");

html:

<select name="Countries"><option selectd> Please select</option>
  <option value="brasil">Brasil</option>
  <option value="portugal">Portugal</option>
  <option value="am">America</option>
  <option value="america">United States</option>
</select> 

This will select: <option value="br">Brasil</option>

For more info refer to: https://webdriver.io/docs/api/element/selectByVisibleText.html

0 votes

How to select a value from a static dropdown in Selenium?

  1. selectByVisibleText(String args) This method is most commonly used in dropdowns. ...
  2. selectByIndex(String args)
  3. This method takes the index of the option to select in the dropdown. ...
  4. Syntax − Select s = new Select(driver.findElement(By.id("<< id exp>>"))); s.selectByIndex(1);
  5. selectByValue(String args)

For further understanding, you can refer to the Selenium Certification.

answered Dec 16, 2020 by Gitika
• 65,950 points
0 votes
So follow this steps for solution:
First click on this driver. findElement(By. xpath("//input[@id='exampleInput']")). click();
Then select/click your li element same as above example. driver. findElement(By. xpath("li xpath")). click();
You can get more clarity on using the <Select> class, just look at the below Java code. WebElement dropdown = driver. findElement(By.id("Browsers")); Select select = new Select(dropdown); // Alternatively, you can shorten the same code as given below. Select select = new Select(driver.
answered Dec 16, 2020 by Roshni
• 10,480 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,360 points
5,976 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

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

webDriver.switchTo().alert() it inbuild selenium only //Store the alert ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
6,953 views
0 votes
2 answers

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,500 points
6,033 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
6,465 views
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,890 points
4,094 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
5,067 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,150 points
1,903 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 29, 2019 in Selenium by Anvi
• 14,150 points
1,876 views