Tuesday 4 August 2015

How to perform single and multiple selection from Dropdown in Selenium Webdriver using java.

HTML CODE:
<html>
<head>
<title>Selenium WebDriver- Drop Down Interaction</title>
</head>
<body>
<select multiple="multiple" id="language">
<option value="java">Java</option>
<option value="c">C</option>
<option value="perl">Perl</option>
<option value="php">PHP</option>
<option value="python">Python</option>
<option value="javascript">JavaScript</option>
</select>
</body>
</html>

Java code:

import java.util.List; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.Dimension; 
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 Singlemultiselection { 
 
public static void main(String args[]) throws InterruptedException { 
// Initialize Firefox Webdriver       
WebDriver driver = new FirefoxDriver(); 
//Resize current browser window on width and height 
driver.manage().window().setSize(new Dimension(400, 400)); 
//Go to desire page       
driver.get("file:///C:/Users/Hiro%20Mia/Desktop/selection.html"); 
//Set  timeout       
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
 
// select ByVisible Text 
Select dropdown = new Select(driver.findElement(By.id("language"))); 
dropdown.selectByVisibleText("Java"); 
//Get the first selected option in this select tag  
System.out.println(dropdown.getFirstSelectedOption().getText()); 
Thread.sleep(4000); 
// Deselect all options that display text matching the argument. 
dropdown.deselectByVisibleText("Java"); 
 
//Select all options that have a value matching the argument 
dropdown.selectByValue("php"); 
//Get the first selected option in this select tag  
System.out.println(dropdown.getFirstSelectedOption().getText()); 
Thread.sleep(4000); 
dropdown.deselectByValue("php"); 
 
//Select the option at the given index. 
dropdown.selectByIndex(4); 
//Get the first selected option in this select tag  
System.out.println(dropdown.getFirstSelectedOption().getText()); 
Thread.sleep(4000); 
// Deselect the option at the given index 
dropdown.deselectByIndex(4); 
 
// multiple selection 
dropdown.selectByVisibleText("Java"); 
dropdown.selectByValue("php"); 
dropdown.selectByIndex(4); 
// Return all selected options belonging to this select tag 
System.out.println("=================="); 
List<WebElement> allselectedOptions = dropdown.getAllSelectedOptions(); 
for (WebElement webElement : allselectedOptions) { 
    System.out.println(webElement.getText()); 

System.out.println("=================="); 
//Check multiple select 
System.out.println(dropdown.isMultiple()); 
Thread.sleep(4000); 
 
//Get all options belonging to this select tag  
System.out.println("=================="); 
List<WebElement> allOptions = dropdown.getOptions(); 
for (WebElement webElement : allOptions) { 
    System.out.println(webElement.getText()); 

 
//close firefox browser   
driver.quit(); 
    } 

No comments:

Post a Comment