forked from Sonal0409/8PMJulyBatchJava-SeleniumPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDropDownDemo.java
More file actions
114 lines (47 loc) · 2.17 KB
/
DropDownDemo.java
File metadata and controls
114 lines (47 loc) · 2.17 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package seleniumScripts;
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 DropDownDemo {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\Users\\vishal mittal\\Downloads\\chromedriver_win32 (12)\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize(); // maximize the browser window
driver.manage().deleteAllCookies();
driver.get("https://ironspider.ca/forms/dropdowns.htm");
// Select is a selenium class used when we want to test static drop down menus
WebElement e= driver.findElement(By.xpath("//*[@id=\"Content\"]/div[1]/center[1]/div/form/select"));
Select dd = new Select(e);
//Select dd = new Select(driver.findElement(By.xpath("//*[@id=\"Content\"]/div[1]/center[1]/div/form/select")););
dd.selectByIndex(0); // this method will select the option at index 0 in the drop down
Thread.sleep(3000);
dd.selectByValue("sugar"); // this method will select the option based on value attribute in Options tag
Thread.sleep(3000);
dd.selectByVisibleText("With cream & sugar"); // this method will select the option based on text in the drop down
// counting number of options in the drop down
// List<String> s1;
// List<Integer> in;
// List Object
List<WebElement> li=dd.getOptions(); // this method will fetch all the options (5) from the drop down
int num =li.size(); // will count the total number of values in the list
System.out.println("Total values in DD : "+ num);
//printing all the values from the drop down on your console
// write a for loop
for(int i=0; i<num;i++)
{
System.out.println(li.get(i).getText());
}
// OR
for(WebElement i : li)
{
String x= i.getText();
if(x.equals("with sugar"));
System.out.println(x);
break;
}
}
}