0

我知道这个问题听起来像是重复,但我已经尝试了在这里找到的解决方案,但到目前为止没有一个对我有用。

从以下问题实施解决方案:

https://stackoverflow.com/questions/7489917/using-selenium-2-and-firefox-how-do-you-select-a-dropdown-selection Selenium WebDriver 和下拉框

Ruby 中的方法参考:http: //selenium.googlecode.com/svn/trunk/docs/api/rb/index.html#selenium-webdriver

我正在使用 selenium 和黄瓜来测试网站。我在 IE 8 中工作。

chan_text = @driver.find_element(:css,'select#check-list option').text
select_list = @driver.find_element(:css,'select#check-list')
dropdown = Selenium::WebDriver::Support::Select.new(select_list)
dropdown.select_by(:text, chan_text)

源代码如下所示:

<select id="check_list" multiple="">
<option selected="" value="81" label="MILK">MILK</option>
<option value="82" label="CHEESE">CHEESE</option>
<option value="83" label="DOUGHNUTS">DOUGHNUTS</option>
</select>

选择一个选项后,页面应刷新。这根本没有发生,菜单只是保持打开状态。任何帮助表示赞赏,如果需要,请请求更多信息。如果它真的是重复的,我将删除这个问题。

4

2 回答 2

1

如果你这样做,你会得到什么?

chan_text = @driver.find_element(:css,'select#check-list option').text
puts chan_text

我敢打赌,不是你要找的。

但如果你这样做——

chan_text = @driver.find_element(:css,'select#check-list option:nth-child(2)').text
puts chan_text #"CHEESE" is what will be returned

原因是您的 find_element 选择器(用于 chan_text)返回所有选项值,我假设您只想要一个。因此,附加:nth-child(n)在您的 css 选择器中,将帮助您选择您特别感兴趣的选项。

于 2013-06-26T16:52:07.883 回答
1

这是我发现的一个更好的选择:

#Select the dropdown button 
dropdown_list = driver.find_element(:id, 'check_list')

#Get all the options from the dropdown
options = dropdown_list.find_elements(tag_name: 'option')

#Find the dropdown value by text
options.each { |option| option.click if option.text == 'CHEESE' }
于 2016-03-10T04:10:08.083 回答