public class StaticDropdown {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Downloads\\chromedriver-win64 (1)\\chromedriver-win64\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://rahulshettyacademy.com/dropdownsPractise/");
WebElement dd= driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency"));
//click on Dropdown
dd.click();
Select dropdn =new Select(dd);
//Select the value from dropdown which position is second
dropdn.selectByIndex(1);
}
public class StaticDropdown {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Downloads\\chromedriver-win64 (1)\\chromedriver-win64\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://rahulshettyacademy.com/dropdownsPractise/");
WebElement dd= driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency"));
//click on Dropdown
dd.click();
Select dropdn =new Select(dd);
//Select the value from dropdown which position is second
dropdn.selectByIndex(2);
WebElement en= dropdn.getFirstSelectedOption();
String value =en.getText();
if (value.equals("AED"))
{
System.out.println("Right");
}
else {
System.out.println("Wrong");
}
- selectByIndex()
- SelectByValue()
- selectByVisibleText()
Selenium Code//How to select a value from the dropdown -index
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
dropd.selectByIndex(4);
Selenium Code//How to select the value from dropdown -value
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
dropd.selectByValue("Manual");
Selenium Code//How to select the value from dropdown - Visible Text
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
dropd.selectByVisibleText("Database Testing");
- getOptions()
- getFirstSelectedOptions()
- getAllSelectedOptions()
Selenium Code/How to get all the options from dropdownSelect dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
List<WebElement> list = dropd.getOptions();
System.out.println("The dropdown options are:");
for(WebElement options: list)
System.out.println(options.getText());
Selenium Code//How to get the first Selected option of dropdownSelect dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
WebElement firstop =dropd.getFirstSelectedOption();
String val=firstop.getText();
System.out.println(val);
Using the above code we can retrieve and print the first selected options of a dropdown.
Selenium Code//How to get all selected options of dropdown
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
List<WebElement> list = dropd.getAllSelectedOptions();
for(WebElement options: list)
System.out.println(options.getText());
Using the above code we can retrieve and print all selected options of a dropdown.
Select class in the selenium web driver provides multiple ways to deselect value from the dropdown.
Selenium Code//How to deselect all value from dropdown
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
dropd.deselectAll();
Selenium Code//How to deselect value from dropdown -index
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
dropd.deselectByIndex(4);
Selenium Code//How to deselect value from dropdown -value
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
dropd.deselectByValue("Manual");
Selenium Code//How to deselect value from dropdown -visibletext
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
dropd.deselectByVisibleText("Manual Testing");
Selenium Code-1//To verify whether dropdown is multiselect
Select dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
if(dropd.isMultiple())
{
System.out.println("YES dropdon is multiselect");
}
else
{
System.out.println("NO dropdown is not multiselect");
}
Selenium Code-2//To check that dropdown is multi-selectSelect dropd = new Select(driver.findElement(By.xpath("//select[@id='testingDropdown']")));
boolean hd=dropd.isMultiple();
System.out.println(hd);
0 Comments