Learn Selenium and Java for Selenium

Saturday, 16 April 2016

Selecting a option from Dropdown List in Selenium

How to select option from Drop Down List


How Selenium supports DropDown List?


  • Selenium webdriver provides a class called Select from package "org.openqa.selenium.support.ui.Select
  • Drop down Menu
  • Interestingly Select Class has 3 methods by which dropdown list values can be selected they are
    • selectByIndex():Selection will be made on the indexing of the list.First member of list will have index as zero and so on it will be incremented for the next members.
    • selectByValue():Selection will be made on the basis on numbering of the list
    • selectByVisibleText(): Selection will be made of the text visible on the drop down list

The following code will select Date/Month/Year on the facebook login page by using the methods described above

 import java.io.IOException;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 import org.openqa.selenium.support.ui.Select;  
 public class Dropdown {  
      public static void main(String[] args) throws IOException, InterruptedException {  
      WebDriver a= new FirefoxDriver();  
      a.manage().window().maximize();  
      a.get("http://www.facebook.com/");  
      Select index= new Select(a.findElement(By.xpath(".//*[@id='day']")));  
      index.selectByIndex(1);  
      Select month= new Select(a.findElement(By.xpath(".//*[@id='month']")));  
      month.selectByVisibleText("Jan");  
      Select year= new Select(a.findElement(By.xpath(".//*[@id='year']")));  
      year.selectByValue("1989");  
      a.quit();  
      }  
      }  

'

Notes

1- As Select is a class  3 different types of objects has been created to cater different type of dropdown list for day/month/year
2- Values on the drop down are accessed via Index/Visible text/Value


Output

0 comments:

Post a Comment

Popular Posts

Recent Posts



Newsletter

Join our newsletter to get secret tips directly in your inbox!


*We Hate Spam!

Powered by Blogger.