Single Post

Header

Sunday, November 1, 2015

Select value from dropdown list in selenium

Select a value from DropDown list using Selenium

Steps to select a value from through selenium
1.Open the website
2.Create an object for the dropdown list
3.Convert the above object into Select object
4.Select a value By Index/Value/Text

package SeleniumPractise;


import java.util.List;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class DisplayDropDownListValues {

 @Test
 public void displayDropDownList() throws InterruptedException {
  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.spicejet.com/");
  driver.manage().window().maximize();
  Thread.sleep(4000);
  // Get the Drop Down element and create an object for that.
  WebElement button = driver.findElement(By.cssSelector(".flight_status"));
  button.click();
  Thread.sleep(4000);
  // Switch to the framework
  driver.switchTo().frame("RSIFramestatus");
  WebElement element = driver.findElement(By.name("originStation"));
     List<WebElement> values = element.findElements(By.tagName("Option"));
  for(WebElement value:values) {
      System.out.println("city : "+value.getText());
     }
        driver.close();

 }
 @Test
 public void dropDownListDenstinationValues() throws InterruptedException {
   WebDriver driver = new FirefoxDriver();
   driver.get("http://www.spicejet.com/");
   driver.manage().window().maximize();
   Thread.sleep(2000);
   WebElement button = driver.findElement(By.cssSelector(".flight_status a"));
   button.click();
   Thread.sleep(3000);
   driver.switchTo().frame("RSIFramestatus");
   WebElement element = driver.findElement(By.name("originStation"));
   Select selectObject = new Select(element);
   selectObject.selectByVisibleText("Bengaluru (BLR)");
   System.out.println("The selected value is"+selectObject.getFirstSelectedOption().getText());
   Thread.sleep(3000);
   WebElement destination = driver.findElement(By.name("destinationStation"));
   Select selectDestination = new Select(destination);
   selectDestination.selectByValue("DEL");
   Thread.sleep(2000);
   driver.close();
 }

 @Test
 public void selectValueFromdropDownList() throws InterruptedException {
  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.spicejet.com/");
  driver.manage().window().maximize();
  Thread.sleep(3000);

  // Get the Drop Down list element and create an object for that.
  // <select id="ctl00_mainContent_ddl_Child">
  WebElement element = driver.findElement(By.id("ctl00_mainContent_ddl_Child"));
  // Convert the element into Select object
  Select selectObject = new Select(element);
  selectObject.selectByIndex(2);     //Select by Index                              
  Thread.sleep(3000);
 // <option value="3">3 Children</option>
  selectObject.selectByValue("3");     //Select by Value                          
  Thread.sleep(3000);
 // <option value="1">1 Child</option>
  selectObject.selectByVisibleText("1 Child");   //Select by Visible text
Thread.sleep(3000);
  // To print the above selected value in console window
  System.out.println("The selected value is"+selectObject.getFirstSelectedOption().getText());
  driver.close();

 }
}


No comments:

Post a Comment