簡體   English   中英

如何使用Selenium / Webdriver查找禁用的選項

[英]How to find out disabled option with Selenium/Webdriver

我有一個帶有下拉菜單的頁面,其中將禁用一些元素。 我想遍歷下拉值以檢查它們是啟用還是禁用。 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>

可能的解決方案:

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

回答我自己的問題

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

這是一個基於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"
driver.findElement(By.xpath("//select[@id='s']/select[@disabled]"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM