0
<select name="dropdown" multiple="multiple" size="2">

  <option value="1">#Ram</option>
  <option value="2">##animals</option>
  <option value="3">###cat</option>
  <option value="4">###dog</option>
  <option value="5">#kalu</option>
  <option value="6">##animals</option>
  <option value="7">###rat</option>
  <option value="8">###dog</option>
  <option value="9">#jadu</option>
  <option value="10">##animals</option>
  <option value="11">###cat</option>

</select>

现在我想选择###dog#kalu->##animals->###dog 之后的 select ——这是我要给出的输入。但现在的重点是如何选择正确###dog

4

2 回答 2

1

我认为您必须获得所有选项并确定每个选项的路径(即父母)。

以下内容适用于您的 html 示例。希望我没有做出太多假设。

#This is the option (path) you want to select
option_to_select = '#kalu->##animals->###dog'

#Get the select list
dropdown = Selenium::WebDriver::Support::Select.new(driver.find_element(:name => 'dropdown'))

#Get the text of all options
option_text = dropdown.options.collect(&:text)

#For each option, determine its full path and value
paths = {}
option_text.each_with_index do |text, index|
    level = text[/^(#+)/].length
    segment = option_text[0..index]
    path = []
    1.upto(level) do |i|
        path << segment[segment.rindex{ |x| x =~ /^#{'#' * i}\w/ }]
    end
    paths[path.join('->')] = (index + 1).to_s
end

#Convert the desired option path to a value and select it from the dropdown
dropdown.select_by(:value, paths[option_to_select])
于 2013-02-06T02:53:24.963 回答
0

Sizzle 必须支持 CSS 选择器:

driver.find_element(:css, "select option:contains('###dog'):last")
于 2013-02-04T23:34:16.547 回答