-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropDown
38 lines (33 loc) · 1.5 KB
/
DropDown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.List;
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 DropDown {
public static void main(String args[]) {
System.setProperty("webdriver.chrome.driver", "/Users/...");
WebDriver driver = new ChromeDriver();
driver.get("https://demoqa.com/select-menu");
driver.manage().window().maximize();
Select select=new Select(driver.findElement(By.id("oldSelectMenu")));
List<WebElement> lst=select.getOptions();
//Looping through the options and printing dropdown options
System.out.println("The dropdown options are:");
for(WebElement options:lst)
System.out.println(options.getText());
//Step#5- Selecting the option as 'Purple'-- selectByIndex
System.out.println("Select the Option by Index 4");
select.selectByIndex(4);
System.out.println("Select value is: " + select.getFirstSelectedOption().getText());
//Step#6- Selecting the option as 'Magenta'-- selectByVisibleText
System.out.println("Select the Option by Text Magenta");
select.selectByVisibleText("Magenta");
System.out.println("Select value is: " + select.getFirstSelectedOption().getText());
//Step#7- Selecting an option by its value
System.out.println("Select the Option by value 6");
select.selectByValue("6");
System.out.println("Select value is: " + select.getFirstSelectedOption().getText());
driver.quit();
}
}