This is the website I am practicing on https://rahulshettyacademy.com/dropdownsPractise/
This is my code WebElement origin=driver.findElement(By.xpath("//*[@id="ctl00_mainContent_ddl_originStation1"]")); Select ori=new Select(origin); List oriui= ori.getOptions(); int size = oriui.size(); for(int i =0; i<size ; i ){ String options = oriui.get(i).getText(); System.out.println(options);
CodePudding user response:
Let's beautify your code first. https://codebeautify.org/javaviewer
WebElement origin = driver.findElement(By.xpath("//*[@id="ctl00_mainContent_ddl_originStation1 "]"));
Select ori = new Select(origin);
List oriui = ori.getOptions();
int size = oriui.size();
for (int i = 0; i < size; i ) {
String options = oriui.get(i).getText();
System.out.println(options);
First, to get the select element properly, pass the XPath as a String
. Use the single quote or escape character as the element ID in the xpath also a nested String in this case.
Be like..
driver.findElement(By.xpath("//*[@id='ctl00_mainContent_ddl_originStation1']"));
Alternatively you can use By.id
if you are calling it by ID only.
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1"));
public void Test() throws Exception {
String URL = "https://rahulshettyacademy.com/dropdownsPractise/";
driver.get(URL);
WebElement origin = driver.findElement(By.id("ctl00_mainContent_ddl_originStation1"));
Select ori = new Select(origin);
List<WebElement> oriui = ori.getOptions();
for(WebElement ele : oriui) {
System.out.println(ele.getAttribute("text"));
}
}
OUTPUT:
Departure City
Adampur (AIP)
Ahmedabad (AMD)
Amritsar (ATQ)
......
Srinagar (SXR)
Vijayawada (VGA)
Vishakhapatnam (VTZ)