0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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"));

4 Comments

thanks bro now its working, but i want to know why for each is not working?
try printing the value of option1 in For Each loop and you would see the issue.
you can do so but i think there is problem in your xpath - WebElement country = driver.findElementByXPath("//select[@id='dropdown_7']"); I haven't seen method findElementByXPath in java
Its in selenium remote webdriver.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.