Monday, February 15, 2016

Working with WebList (Drop Down List)

Go to site http://www.shine.com/partnersites/
and search for Testing and location as Bangalore
And display all the options available under the drop dow list
And using Select class select the particular value from drop down by method dropdown.selectByVisibleText


Script : 


package com.session4;
import java.util.List;
import javax.swing.plaf.synth.SynthSeparatorUI;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class WebList {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","yourpath/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.shine.com/partnersites/");
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id='id_q']")).sendKeys("Testing");
//*[@id="id_loc"]
driver.findElement(By.id("id_loc")).sendKeys("Bangalore");
// interact with WebList -- drop down..
// display all the values in drop down list,,
List<WebElement> d = driver.findElement(By.id("id_minexp")).findElements(By.tagName("option"));
System.out.println(d.size());
// how to print all the drop down values..
for(int i=0;i<d.size();i++){
System.out.println(d.get(i).getText());
}
// end of for loop..
Select dd = new Select(driver.findElement(By.id("id_minexp")));
//dd.selectByVisibleText("3 Yrs");
//dd.selectByIndex(5);
dd.selectByValue("6");
// other way of getting all the dropdown values...
System.out.println("****************");
WebElement mySelectElement = driver.findElement(By.id("id_minexp"));
Select dropdown= new Select(mySelectElement);
List<WebElement> options = dropdown.getOptions();
for (WebElement option : options) {
System.out.println(option.getText()); //output "option1", "option2", "option3"
}
}
}
view raw WebList.java hosted with ❤ by GitHub