尝试点击并选择Capybara的下拉菜单

时间:2014-01-09 17:02:09

标签: selenium-webdriver cucumber capybara

尝试点击并选择Capybara的下拉菜单。但下拉列表是动态的,没有硬编码选项。到目前为止,在我的尝试中,我无法从中选择任何选项。我正在尝试在下拉列表中选择第一个或第二个值。 HTML是:

<span class="dojoComboBoxOuter dj_gecko" _="whitespace and CR's between tags adds   in FF" style="width: 400px;">

<input dojoattachpoint="comboBoxValue" value="" name="personSelect" tabindex="-1" style="display:none"></input>
<input dojoattachpoint="comboBoxSelectionValue" value="" name="personSelect_selected" tabindex="-1" style="display:none"></input>
<input class="dojoComboBox" type="text" dojoattachpoint="textInputNode" dojoattachevent="key:_handleKeyEvents; keyUp: onKeyUp; compositionEnd; onResize;" autocomplete="off" style="width: 400px;"></input>
<img class="dojoComboBox" vspace="0" hspace="0" src="/ess/assets/static/dojo-0.4.3-custom-4.1.6/src/widget/templates/images/combo_box_arrow.png" dojoattachevent="onMouseUp: handleArrowClick; onResize;" dojoattachpoint="downArrowNode" style="width: 14px; height: 14px;"></img>

</span>

下拉列表的小箭头是最后一个img类,它被称为“combo_box_arrow.png”,试图选择但没有运气。

思想?

2 个答案:

答案 0 :(得分:0)

我不知道水豚,但一般的想法是点击下拉图片,然后找到组合框下的所有输入元素,并选择您要选择的那个。这就是我在Java中的表现。我相信你可以重用css选择器来查找capybara中的元素。

WebElement drop_down_image = driver.findElement(By.cssSelector("img.dojoComboBox"));
drop_down_image.click();
List<WebElement> options = driver.findElements(By.cssSelector("span.dojoComboBoxOuter>input[name^='personSelect']"));

for(WebElement option:options) {
      if(option.getText().equals("Drop down I want to select")) {
          option.click();
          break;
      }   
}

请注意,我在这里直接在编辑器中编写了这段代码,因此可能存在编译错误,但是你明白了!

答案 1 :(得分:0)

Capybara不会(默认情况下)允许您与隐藏元素进行交互,因此您需要单击图像然后与输入进行交互。像这样:

When /^(?:|I )choose the (\d+)(?:st|nd|rd|th) item from the dropdown"$/ do |position|
  position = position.to_i - 1
  within '.dojoComboBoxOuter' do
    all('input').at(position).click
  end
end