Reputation: 123
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>
Upvotes: 3
Views: 13160
Reputation: 41
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.
Upvotes: -1
Reputation: 31
Select comboBox = new Select(webDriver
.findElementById(comboBoxId));
comboBox.selectByVisibleText(optionText);
Upvotes: 3
Reputation: 9294
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..
Upvotes: 1
Reputation: 17858
You should find your select
element first and then iterate through its option
s
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");
}
}
Upvotes: 0