【问题标题】:Cannot select item in drop down list无法在下拉列表中选择项目
【发布时间】:2013-03-04 20:17:30
【问题描述】:

我有一个无法从中选择项目的下拉列表。我可以遍历列表中的所有项目并找到我想要的项目,但 click() 没有选择项目。

这里是代码。有人可以帮忙吗?

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

这里是组件下拉列表的html代码。

<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>

【问题讨论】:

  • 你能发布包含列表的部分 html 吗?该列表似乎是某种 3d 派对小部件

标签: selenium-webdriver


【解决方案1】:
Select comboBox = new Select(webDriver
      .findElementById(comboBoxId));
comboBox.selectByVisibleText(optionText); 

【讨论】:

    【解决方案2】:

    我想你现在已经看到了,但本教程显示了一个选择选项的示例,如下所示:

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

    所以不要调用 click 你应该调用 setSelected 方法

    你也可以使用

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

    更多信息在这里:http://seleniumhq.org/docs/09_webdriver.html

    我仍然对您的问题感到困惑,因为您发布了一些包含选项列表的 html,但在您的代码中,您按类名查找了一个在您的 html 中不存在的元素。也许您只是想点击某种下拉菜单而不是选择框选项..

    【讨论】:

    • 这是一个下拉列表,如果我使用 setSelected(),我会收到错误,您可能无法选择不可选择的元素。
    • 如果可以,尝试制作一个真正简单的网页,它是一个带有选择框的简单 html 页面,看看是否可以重现该行为。如果可以,请写下问题作为 selenium/webdriver 人员的错误,看看他们是否可以修复它。我没有使用 java 客户端,但使用 ruby​​ 对我来说效果很好。
    【解决方案3】:

    你应该首先找到你的select元素,然后遍历它的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");
        }
    }
    

    【讨论】:

    • 我尝试了您的代码,现在当我到达 component.click() 时,我收到元素不可见的错误。
    • 使用您提供的 HTML 代码可以正常工作。你可以发布CSS吗?这是您的组织开发的小部件,还是您使用的是第三方的小部件? (道场、YUI 等)
    【解决方案4】:

    如果您出于某种目的尝试触发 onselect 事件,您可以使用 sendkeys("\t)。即模拟元素的跳出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多