如何找到Selenium / Webdriver的禁用选项

时间:2013-12-19 15:13:06

标签: java ruby selenium selenium-webdriver cucumber

我有一个带有下拉列表的页面,它将禁用少量元素。我想循环显示下拉值以检查它们是启用还是禁用。是否可以使用Selenium / Webdriver?

<body>
  <select id="s">
    <option value="0" selected="selected">0</option>
    <option value="1" disabled>1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
  </select>
</body>

4 个答案:

答案 0 :(得分:2)

可能的解决方案:

WebElement temp = driver.findElement(By.id("s"));
List<WebElement> opts = temp.findElements(By.xpath(".//option"));
for (WebElement opt : opts){
    if (opt.isEnabled()){
        // do something
    }
}

答案 1 :(得分:1)

回答我自己的问题

driver.get("\\test.html");
Select select = new Select(driver.findElement(By.id("s")));
System.out.println(select.getOptions().get(1).getAttribute("disabled")); #=> true

答案 2 :(得分:1)

这是一个基于Ruby的解决方案:

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get "file:///C:/userdata/arupruby/test.html"

# To search the drop-down element whose id is 's'
elem = driver.find_element(:id,'s')
sel = Selenium::WebDriver::Support::Select.new(elem)

# To search if any element is present which has the disabled attribute.
dis_elem = sel.options.find{|e| e.attribute('disabled') }
dis_elem.text unless dis_elem.nil? # => "1"

答案 3 :(得分:1)

driver.findElement(By.xpath("//select[@id='s']/select[@disabled]"));