3

I have a drop down list that I cannot select an item from. I can loop through all the items in the list and find the one I want but the click() does not select the item.

Here is the code. Can any one help?

driver.findElement(By.id("components-multi-select")).findElement(By.className("icon")).click(); driver.findElement(By.id("components-suggestions")); List<WebElement> componentList = driver.findElements(By.className("aui-list-item")); for (WebElement component : componentList){ System.out.println(component.getText()); if (component.getText().contains(newComponent)){ component.click(); break; } else{ System.out.println("not equal"); } 

Here is the html code of the component drop down list.

<div class="field-group aui-field-componentspicker frother-control-renderer" > <label for="components">Component/s</label> <div class="ajs-multi-select-placeholder textarea long-field"></div> <select class="select hidden " id="components" multiple="multiple" name="components" size="5" data-remove-null-options="true"> <option value="-1"> Unknown </option> <option selected="selected" title="Component 1 - A test component" value="10240"> Component 1 </option> <option title="Component 2 - " value="10242"> Component 2 </option> <option title="Lee 2 " value="10371"> Lee 2 </option> <option title="Roy " value="10370"> Roy </option> <option title="Test Documentation " value="10241"> Test Documentation </option> </select> 
1
  • Can you post a part of html that contains the list? It seems that the list is some sort of 3d-party widgetCommentedNov 25, 2010 at 12:40

4 Answers 4

3
Select comboBox = new Select(webDriver .findElementById(comboBoxId)); comboBox.selectByVisibleText(optionText); 
    1

    I would imagine you've seen this by now, but the tutorial shows an example of selecting options like so:

    WebElement select = driver.findElement(By.xpath("//select")); List<WebElement> allOptions = select.findElements(By.tagName("option")); for (WebElement option : allOptions) { System.out.println(String.format("Value is: %s", option.getValue())); option.setSelected(); } 

    So instead of calling click you should call the setSelected method

    Also you can use

    Select select = new Select(driver.findElement(By.xpath("//select"))); select.deselectAll(); select.selectByVisibleText("Edam"); 

    More info here: http://seleniumhq.org/docs/09_webdriver.html

    I'm still confused by your question because you posted some html that has a list of options but in your code you lookup an element by classname which does not exist in your html.. Perhaps you are just trying to click some sort of dropdown menu and not a select box option..

    2
    • It is a drop down list and if I use the setSelected(), I get the error, You may not select an unselectable element.
      – John
      CommentedJan 13, 2011 at 18:10
    • If you can, try to make a real simple webpage that is a bare-bones html page with a selectbox in it and see if you can reproduce the behavior there.. If so, write the issue up as a bug for the selenium/webdriver people and see if they can get it fixed. I'm not using the java client but it works fine for me with ruby.CommentedJan 14, 2011 at 1:47
    0

    You should find your select element first and then iterate through its options

    WebElement selectElement = driver.findElement(By.id("components")); List<WebElement> componentList = selectElement.findElements(By.tagName("option")); for (WebElement component : componentList){ System.out.println(component.getText()); if (component.getText().contains(newComponent)){ component.click(); break; } else{ System.out.println("not equal"); } } 
    2
    • I tried your code and now when I get to component.click(), I get the error that the Element is not visible.
      – John
      CommentedDec 9, 2010 at 18:30
    • Using the HTML that you provided my code works fine. Can you post the CSS? Is this the widget developed by your organization or are you using some third-party one? (Dojo, YUI, etc)CommentedDec 10, 2010 at 9:46
    -1

    If you are trying to trigger an onselect event for some purpose, you can use sendkeys("\t). ie to simulate tabbing out of the element.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.