SELECT : class selenium.webdriver.support.select.Select(webelement)
There are three ways you can select and deselect an element.
NOTE : A check is made to the given element that the tag is a SELECT Tag i.e
<select id="year" class="_5dba" title="Year" name="birthday_year" aria-label="Year">
If this Select tag is not there then we cannot use the Select class . It will throw an exception as "UnexpectedTagNameException
NOTE: Select class only works for elements with <select> tags.
Now, once you have created a SELECT object, you can access all the methods resides in side the SELECT class by typing Select + dot.
In an HTML code a drop down looks like this :
The following code will print all the available options present in the drop down:
package dropDownSelection;
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;
import org.testng.annotations.Test;
public class PrintElementsInDropDown {
WebDriver driver;
@Test
public void dropDownExample() {
driver = new FirefoxDriver();
driver.get("http://facebook.com");
Select Select = new Select(driver.findElement(By.id("month")));
// Select.getOptions() will find the available options present in the drop down list and place it in a list
List<WebElement> elementCount = Select.getOptions();
//To print the count of elements in the drop down list
int iSize = elementCount.size();
System.out.println("Total Number of item count in dropdown list = " + iSize);
// To print all the options in the drop down list
for (int i = 0; i <= iSize; i++) {
String sValue = elementCount.get(i).getText();
System.out.println(sValue);
}
}
}
Executing test via TESTNG.xml :
<?xml version="1.0" encoding="UTF-8"?>
<suite name="dropdown in selenium webdriver">
<test name="dropdown test">
<classes>
<class name="dropDownSelection.PrintElementsInDropDown" />
</classes>
</test>
</suite>
OUTPUT:
Total Number of item count in dropdown list = 13
Month
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sept
Oct
Nov
Dec
===============================================
dropdown in selenium webdriver
Total tests run: 1, Failures: 1, Skips: 0
===============================================
SELECT BY VISIBLE TEXT COMMAND:
EXAMPLE:
package dropDownSelection;
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;
import org.testng.annotations.Test;
public class SelectByVisibleText {
WebDriver driver;
@Test
public void selectByVisibleTextExample() {
driver = new FirefoxDriver();
driver.get("http://facebook.com");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.id("month"));
Select se = new Select(element);
se.selectByVisibleText("May");
WebElement option = se.getFirstSelectedOption();
String SelectedMonth = option.getText();
if (SelectedMonth.equalsIgnoreCase("May")) {
System.out.println("Able to select the selected month in drop down");
} else {
System.out.println("Unable to select the selected month in drop down");
}
}
}
Executing test via TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="dropdown in selenium webdriver">
<test name="SelectByValue test">
<classes>
<class name="dropDownSelection.SelectByVisibleText" />
</classes>
</test>
</suite>
OUTPUT:
[TestNG] Running:
E:\AutomationCode19012016WorkSpace\SeleniumAllOperations\src\dropDownSelection\TestNg.xml
Able to select the selected month in drop down
===============================================
dropdown in selenium webdriver
Total tests run: 1, Failures: 0, Skips: 0
===============================================
SELECT BY INDEX COMMAND:
EXAMPLE:
package dropDownSelection;
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;
import org.testng.annotations.Test;
public class SelectByIndex {
WebDriver driver;
@Test
public void selectByIndexExample() {
driver = new FirefoxDriver();
driver.get("http://facebook.com");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.id("month"));
Select se = new Select(element);
se.selectByIndex(2);
WebElement option = se.getFirstSelectedOption();
String SelectedMonth = option.getText();
if (SelectedMonth.equalsIgnoreCase("Feb")) {
System.out.println("Able to select the selected index in drop down");
} else {
System.out.println("Unable to select the selected index in drop down");
}
}
}
Executing test via TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="dropdown in selenium webdriver">
<test name="SelectByValue test">
<classes>
<class name="dropDownSelection.SelectByIndex" />
</classes>
</test>
</suite>
OUTPUT:
Able to select the selected index in drop down
===============================================
dropdown in selenium webdriver
Total tests run: 1, Failures: 0, Skips: 0
===============================================
SELECT BY VALUE COMMAND:
EXAMPLE:
package dropDownSelection;
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;
import org.testng.annotations.Test;
public class SelectByValue {
WebDriver driver;
@Test
public void selectByValueExample() {
driver = new FirefoxDriver();
driver.get("http://facebook.com");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.id("month"));
Select se = new Select(element);
se.selectByValue("1");
WebElement option = se.getFirstSelectedOption();
String SelectedMonth = option.getText();
if (SelectedMonth.equalsIgnoreCase("Jan")) {
System.out.println("Able to select the selected value in drop down");
} else {
System.out.println("Unable to select the selected value in drop down");
}
}
}
EXECUTING TEST VIA TESTNG.XML
<?xml version="1.0" encoding="UTF-8"?>
<suite name="dropdown in selenium webdriver">
<test name="SelectByValue test">
<classes>
<class name="dropDownSelection.SelectByValue" />
</classes>
</test>
</suite>
OUTPUT:
Able to select the selected value in drop down
===============================================
dropdown in selenium webdriver
Total tests run: 1, Failures: 0, Skips: 0
===============================================
There are three ways you can select and deselect an element.
- select by value
- by index
- by visible text.
<select id="year" class="_5dba" title="Year" name="birthday_year" aria-label="Year">
If this Select tag is not there then we cannot use the Select class . It will throw an exception as "UnexpectedTagNameException
NOTE: Select class only works for elements with <select> tags.
Now, once you have created a SELECT object, you can access all the methods resides in side the SELECT class by typing Select + dot.
In an HTML code a drop down looks like this :
The following code will print all the available options present in the drop down:
package dropDownSelection;
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;
import org.testng.annotations.Test;
public class PrintElementsInDropDown {
WebDriver driver;
@Test
public void dropDownExample() {
driver = new FirefoxDriver();
driver.get("http://facebook.com");
Select Select = new Select(driver.findElement(By.id("month")));
// Select.getOptions() will find the available options present in the drop down list and place it in a list
List<WebElement> elementCount = Select.getOptions();
//To print the count of elements in the drop down list
int iSize = elementCount.size();
System.out.println("Total Number of item count in dropdown list = " + iSize);
// To print all the options in the drop down list
for (int i = 0; i <= iSize; i++) {
String sValue = elementCount.get(i).getText();
System.out.println(sValue);
}
}
}
Executing test via TESTNG.xml :
<?xml version="1.0" encoding="UTF-8"?>
<suite name="dropdown in selenium webdriver">
<test name="dropdown test">
<classes>
<class name="dropDownSelection.PrintElementsInDropDown" />
</classes>
</test>
</suite>
OUTPUT:
Total Number of item count in dropdown list = 13
Month
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sept
Oct
Nov
Dec
===============================================
dropdown in selenium webdriver
Total tests run: 1, Failures: 1, Skips: 0
===============================================
SELECT BY VISIBLE TEXT COMMAND:
EXAMPLE:
package dropDownSelection;
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;
import org.testng.annotations.Test;
public class SelectByVisibleText {
WebDriver driver;
@Test
public void selectByVisibleTextExample() {
driver = new FirefoxDriver();
driver.get("http://facebook.com");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.id("month"));
Select se = new Select(element);
se.selectByVisibleText("May");
WebElement option = se.getFirstSelectedOption();
String SelectedMonth = option.getText();
if (SelectedMonth.equalsIgnoreCase("May")) {
System.out.println("Able to select the selected month in drop down");
} else {
System.out.println("Unable to select the selected month in drop down");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<suite name="dropdown in selenium webdriver">
<test name="SelectByValue test">
<classes>
<class name="dropDownSelection.SelectByVisibleText" />
</classes>
</test>
</suite>
OUTPUT:
[TestNG] Running:
E:\AutomationCode19012016WorkSpace\SeleniumAllOperations\src\dropDownSelection\TestNg.xml
Able to select the selected month in drop down
===============================================
dropdown in selenium webdriver
Total tests run: 1, Failures: 0, Skips: 0
===============================================
SELECT BY INDEX COMMAND:
EXAMPLE:
package dropDownSelection;
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;
import org.testng.annotations.Test;
public class SelectByIndex {
WebDriver driver;
@Test
public void selectByIndexExample() {
driver = new FirefoxDriver();
driver.get("http://facebook.com");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.id("month"));
Select se = new Select(element);
se.selectByIndex(2);
WebElement option = se.getFirstSelectedOption();
String SelectedMonth = option.getText();
if (SelectedMonth.equalsIgnoreCase("Feb")) {
System.out.println("Able to select the selected index in drop down");
} else {
System.out.println("Unable to select the selected index in drop down");
}
}
}
Executing test via TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="dropdown in selenium webdriver">
<test name="SelectByValue test">
<classes>
<class name="dropDownSelection.SelectByIndex" />
</classes>
</test>
</suite>
OUTPUT:
Able to select the selected index in drop down
===============================================
dropdown in selenium webdriver
Total tests run: 1, Failures: 0, Skips: 0
===============================================
SELECT BY VALUE COMMAND:
EXAMPLE:
package dropDownSelection;
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;
import org.testng.annotations.Test;
public class SelectByValue {
WebDriver driver;
@Test
public void selectByValueExample() {
driver = new FirefoxDriver();
driver.get("http://facebook.com");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.id("month"));
Select se = new Select(element);
se.selectByValue("1");
WebElement option = se.getFirstSelectedOption();
String SelectedMonth = option.getText();
if (SelectedMonth.equalsIgnoreCase("Jan")) {
System.out.println("Able to select the selected value in drop down");
} else {
System.out.println("Unable to select the selected value in drop down");
}
}
}
EXECUTING TEST VIA TESTNG.XML
<?xml version="1.0" encoding="UTF-8"?>
<suite name="dropdown in selenium webdriver">
<test name="SelectByValue test">
<classes>
<class name="dropDownSelection.SelectByValue" />
</classes>
</test>
</suite>
OUTPUT:
Able to select the selected value in drop down
===============================================
dropdown in selenium webdriver
Total tests run: 1, Failures: 0, Skips: 0
===============================================
No comments:
Post a Comment