Selenium DropDownList using Selenium Java
import java.util.List;
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 SelectExample {
WebDriver driver;
public void setup(){
driver=new FirefoxDriver();
// driver.get("http://careerendeavour.com/admission-form/registration-form.php");
driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple");
driver.manage().window().maximize();
}
public void selectDroplist(){
// find the Element for Droplist stored element reference as WebElement
WebElement studyCenter_element=driver.findElement(By.id("center"));
// create Select class object passing Webelement as Constructor param
Select select=new Select(studyCenter_element);
// select class reference, we will call select class methods
//select.selectByIndex(3);
WebElement beforeselectvalue=select.getFirstSelectedOption();
System.out.println("Default selected value--------"+beforeselectvalue.getText());
select.selectByVisibleText("GTB Nagar");
WebElement afterselectvalue=select.getFirstSelectedOption();
System.out.println("After selected value--------"+afterselectvalue.getText());
// select droplist have multiple option choose or not?
boolean isMul=select.isMultiple();
if(isMul){
System.out.println("given droplist is mulitiple seelct option");
}else{
System.out.println("It is single item select onces......");
}
// get and verify the default select option ...
select.getFirstSelectedOption();
}
public void get_AllOptionsAndPrintvalidation(){
// find the Element for Droplist stored element reference as WebElement
WebElement studyCenter_element=driver.findElement(By.id("center"));
// create Select class object passing Webelement as Constructor param
Select select=new Select(studyCenter_element);
// Need to get All option in the droplist
List
//for ==iterating above option list
for(int i=0;i
System.out.println("items is----"+options.get(i).getText());
}
//for each
// for( List Type refval : your list referen )
for( WebElement option : options ){
System.out.println("for each loop iteration-->"+option.getText());
}
}
public void isMutipleDropdlist() throws InterruptedException{
WebElement element=driver.findElement(By.name("cars"));
Select select=new Select(element);
boolean ismul=select.isMultiple();
System.out.println(ismul);
select.selectByIndex(1);
Thread.sleep(6000);
List
for( WebElement r : selectOptions){
System.out.println("Mutiple select droplist---->"+r.getText());
}
}
public static void main(String[] args) throws InterruptedException {
SelectExample se=new SelectExample();
se.setup();
se.isMutipleDropdlist();
}
}
0 Comments