Doubts on Select class | Selenium Forum
M
Posted on 02/12/2015
Hello There,

After watching Module 13 Video, started to work on dropdown list. You have asked to use the
Select class to test the drop down options. Below is the code I have written. Let me know if I am using it in a right way.
1. How to make use of the method selectByIndex, selectByValue to select an option from dropdown.
2. I have written the below code to select the Departure and destination cities from drop downs in makemytrip.com and its not doing as expected.

import java.util.List;
import java.util.concurrent.TimeUnit;

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 Handling_dropdowns {

public static void main(String[] args) {

WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.makemytrip.com/");

WebElement dropdown=driver.findElement(By.id("class_selector"));
//List<WebElement> All=driver.findElements(By.id("class_selector"));
Select s= new Select(dropdown);
s.selectByValue("PE");
driver.findElement(By.id("from_typeahead1")).click();
s.selectByVisibleText("Bangalore, India (BLR)");
driver.findElement(By.id("to_typeahead1")).click();
s.selectByVisibleText("Goa, India (GOI)");

M
Replied on 02/12/2015

[quote:380xxgsf]How to make use of the method selectByIndex, selectByValue to select an option from dropdown[/quote:380xxgsf]

you usage is fine.

[quote:380xxgsf]2. I have written the below code to select the Departure and destination cities from drop downs in makemytrip.com and its not doing as expected.
[/quote:380xxgsf]

try this. just send "enter".

[code:380xxgsf]package com.soapuitutorial.propertie;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
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 Handling_dropdowns {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.makemytrip.com/");

WebElement dropdown = driver.findElement(By.id("class_selector"));
// List<WebElement> All=driver.findElements(By.id("class_selector"));
Select s = new Select(dropdown);
s.selectByValue("PE");
driver.findElement(By.id("from_typeahead1")).sendKeys(
"New Delhi"+Keys.ENTER);
// s.selectByVisibleText("Bangalore, India (BLR)");
driver.findElement(By.id("to_typeahead1")).sendKeys("Goa"+Keys.ENTER);

// s.selectByVisibleText("Goa, India (GOI)");
}
}[/code:380xxgsf]