Reputation: 1
I am not able to select the value in drop down in above mentioned code,
public class Dropdown {
public static void main(String[] args) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "I:/shopclues/demoqa/drivers/chromedriver.exe");
ChromeDriver driver= new ChromeDriver();
driver.get("http://demoqa.com/");
driver.manage().window().maximize();
driver.findElementByLinkText("Registration").click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement country = driver.findElementByXPath("//select[@id='dropdown_7']");
Select dropdown = new Select(country);
List<WebElement> options = dropdown.getOptions();
for (WebElement option1 : options)
{
System.out.println(option1.getText());
if(option1.getText().startsWith("india"))
{
option1.click();
break;
}
}
}
}
Thanks
Upvotes: 0
Views: 1151
Reputation: 7708
Use the following way to select a dropdown -
WebElement country = driver.findElement(By.id("dropdown_7"));
Select dropdown = new Select(country);
dropdown.selectByVisibleText("india"); // pass the text which is visible on site
//or
dropdown.selectByIndex(1); // pass the index of dropdown value you want to select
//or
dropdown.selectByValue("your_value");
Or change your code
WebElement country = driver.findElementByXPath("//select[@id='dropdown_7']");
to
WebElement country = driver.findElement(By.id("dropdown_7"));
Upvotes: 1
Reputation: 61
Based on gihan's answer here, you can select via the following:
dropdown .selectByVisibleText("India"); dropdown.selectByIndex(0); dropdown.selectByValue("India");
For your purposes, the first option should work fine.
Upvotes: 1