Unable to select item in dropdown list

I have a drop-down list from which I cannot select an item. I can iterate over all the items in the list and find the one I want, but click () does not select the item.

Here is the code. Can anyone 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>
+3
source share
4 answers
Select comboBox = new Select(webDriver
      .findElementById(comboBoxId));
comboBox.selectByVisibleText(optionText); 
+3
source

I would suggest that you have already seen this, but the tutorial shows an example of selecting these options:

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();
}

Thus, instead of calling click, you should call the setSelected method

You can also use

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

Further information here: http://seleniumhq.org/docs/09_webdriver.html

, - html, , , html. , - , .

+1

select, 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");
    }
}
0

onselect - , sendkeys ("\ t)), .

-1

Source: https://habr.com/ru/post/1776753/


All Articles