获取布尔属性的实际值 [英] Get the actual value of a boolean attribute

查看:115
本文介绍了获取布尔属性的实际值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有跨度:

<span disabled="disabled">Edit Member</span>

当我尝试获取disabled属性的值时:

When I try to get the value of the disabled attribute:

page.in_iframe(:id => 'MembersAreaFrame') do |frame|
  expect(page.span_element(:xpath => "//span[text()='Edit Member']", :frame => frame).attribute('disabled')).to eq("disabled")
end

我得到:

expected: "disabled"
     got: "true"

如何获取指定属性的值而不是布尔值?

How do I get the value of specified attribute instead of a boolean value?

推荐答案

Page-Object gem的attribute方法不对属性值进行任何格式化.它只是返回从Selenium-WebDriver(或Watir-Webdriver)返回的内容.

The Page-Object gem's attribute method does not do any formatting of the attribute value. It simply returns what is returned from Selenium-WebDriver (or Watir-Webdriver).

对于布尔属性,这意味着将返回true或false.从 Selenium-WebDriver#attribute 文档中:

In the case of boolean attributes, this means that true or false will be returned. From the Selenium-WebDriver#attribute documentation:

以下内容被视为布尔"属性,并将返回 "true"或"false":

The following are deemed to be "boolean" attributes, and will return either "true" or "false":

异步,自动对焦,自动播放,选中,紧凑,完整,控件, 声明,默认选中,默认选中,延迟,禁用,可拖动, 结束,formnovalidate,隐藏,不确定,iscontenteditable, ismap,itemscope,循环,多个,已静音,nohref,noresize,noshade, novalidate,nowrap,打开,暂停,发布,只读,必需, 反向,作用域,无缝,搜索,选定,拼写检查,truespeed, 会验证

async, autofocus, autoplay, checked, compact, complete, controls, declare, defaultchecked, defaultselected, defer, disabled, draggable, ended, formnovalidate, hidden, indeterminate, iscontenteditable, ismap, itemscope, loop, multiple, muted, nohref, noresize, noshade, novalidate, nowrap, open, paused, pubdate, readonly, required, reversed, scoped, seamless, seeking, selected, spellcheck, truespeed, willvalidate

如您所见,"disabled"属性包含在此列表中,因此返回布尔值.

As you can see, the "disabled" attribute is included in this list and thus returns a boolean.

如果您确实要检查实际的属性值,则必须解析HTML.除非HTML很简单,否则我建议您使用Nokogiri(或其他HTML解析器),而不要自己编写.在Nokogiri:

If you really want to check the actual attribute value, you will have to parse the HTML. Unless the HTML is simple, I would suggest using Nokogiri (or other HTML parser) rather than writing your own. In Nokogiri:

require 'nokogiri'

# Get the HTML of the span
span_html = page.in_iframe(:id => 'MembersAreaFrame') do |frame|
  page.span_element(:xpath => "//span[text()='Edit Member']", :frame => frame).html
end

# Parse the span
doc = Nokogiri::HTML.fragment(span_html)
root_element = doc.at_css('*')

# Check the disabled attribute of the root element (ie the span)
expect(root_element['disabled']).to eq("disabled")

这篇关于获取布尔属性的实际值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆