0
\$\begingroup\$

Scenario:Select a any other value other then currently selected from a drop down.

Please help/review the code and tell me whether this is a efficient solution?

public class DropDown {

public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver", "F://chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_option_selected");

    driver.switchTo().frame("iframeResult");

    WebElement dropdownCar = driver.findElement(By.xpath("//select"));
    Select select = new Select(dropdownCar);
    List<WebElement> carValues = select.getOptions();
    WebElement selectedValue = select.getFirstSelectedOption();

    for (int i = 0; i < carValues.size(); i++) {

        if (!(selectedValue.getText().equals(carValues.get(i).getText()))) {

            select.selectByVisibleText(carValues.get(i).getText());
            break;

        }
    }

}

}
\$\endgroup\$
  • \$\begingroup\$ I guess, it will just select first two options. \$\endgroup\$ – Santosh Apr 25 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.