1

Why doesn’t my code reliably click on the dropdown menu item?

  1. My code dosnt reliably click on the intended dropdown menu item.
  2. For example if i execute the same test 100 times, 12 of the tests would fail because the method would not select the intended menu item lets say (Mr), even using send keys the same problem occurs.
  3. I have set a wait time of x30 seconds waiting for the element to be visible, even waiting for the element to be clickable the same problem occurs.
  4. For example please see the following dropwdown item:

    <select id="titlefield" class="form-control ng-pristine ng-untouched ng-invalid ng-invalid-required" name="Salutation" ng-model="PersonalDetails.Salutation" ng-options="salut.id as salut.id for salut in Salutations" ng-required="FlowData.IsGuest" required="required">
    <option class="ng-binding" value="">Please select</option>
    <option value="0" label="Mr.">Mr.</option>
    <option value="1" label="Miss">Miss</option>
    <option value="2" label="Mrs.">Mrs.</option>
    <option value="3" label="Ms.">Ms.</option>
    <option value="4" label="Dr.">Dr.</option>
    

  5. My code is constructed of the following:

    public void selectTitleFromDropdownMenu(WebElement dropdown, String textToSearchFor) {
    Wait<WebDriver> tempWait = new WebDriverWait(this.driver, 30);
    try {
        tempWait.until(ExpectedConditions.visibilityOf(dropdown));
        List<WebElement> options = dropdown.findElements(By.tagName("option"));
        Select selectDropdown = new Select(dropdown);
        for (int i = 0; i < options.size(); i++) {
            if (options.get(i).getText().equals(textToSearchFor)) {
                selectDropdown.selectByVisibleText(textToSearchFor);
                System.out.println("Successfully selected the following text: " + textToSearchFor + ", using the following webelement: " + "<" + dropdown.toString() + ">");
            }
        }
    
    }catch(Exception e) {
        System.out.println("Unable to select the following text: " + textToSearchFor + ", using the following WebElement: " + "<" + dropdown.toString() + ">");
        Assert.assertFalse(true, "Unable to select the required text from the dropdown menu, Exception: " + e.getMessage());
    } 
    

    }

enter image description here

Gbru
  • 1,065
  • 3
  • 24
  • 56
  • Refer to solution provided over here. It will help you out. http://stackoverflow.com/questions/30184055/selenium-select-value-from-a-drop-down-which-is-dependent-on-value-selected-in – Abhinav Feb 17 '17 at 09:50

2 Answers2

0

You need to create the select object on dropdown and not on options. Also, you don't need any for loop.

List<WebElement> options = dropdown.findElements(By.Id("titlefield"));
Select selectDropdown = new Select(dropdown);
selectDropdown.selectByVisibleText(textToSearchFor);
Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137
0

You can try with this also:
Select selectDropdown = new Select(driver.findElement(By.id("titlefield"))); selectDropdown.selectByVisibleText(textToSearchFor);

peter pawar
  • 106
  • 1
  • 9
  • 1
    selectDropdown.selectByVisibleText("textToSearchFor"); -- textToSearchFor should not be in double quotes... Else selenium will be searching for this string rather than the contents of the variable textToSearchFor. – Grasshopper Feb 17 '17 at 09:35