[英]how do I retrieve list of element attributes using watir webdriver
我正在嘗試編寫一個watir webdriver腳本,它檢索元素的屬性然后獲取它們的值。 給定的元素
<input id="foobar" width="200" height="100" value="zoo" type="text"/>
希望我可以做以下事情:
testElement = $b.element(:id, "foobar")
testElement.attributes.each do |attribute|
puts("#{attribute}: #{testElement.attribute_value(attribute)}")
end
我希望得到
id: foobar
width: 200
height: 100
value: zoo
type: text
我見過人們使用javascript來獲取屬性列表。 下面顯示了如何向Watir :: Element添加方法以獲取屬性列表(盡管擴展Watir是可選的)。
#Add the method to list attributes to all elements
require 'watir-webdriver'
module Watir
class Element
def list_attributes
attributes = browser.execute_script(%Q[
var s = [];
var attrs = arguments[0].attributes;
for (var l = 0; l < attrs.length; ++l) {
var a = attrs[l]; s.push(a.name + ': ' + a.value);
} ;
return s;],
self )
end
end
end
#Example usage
browser = Watir::Browser.new
browser.goto('your.page.com')
el = browser.text_field(:id, 'foobar')
puts el.list_attributes
#=> ["width: 200", "type: text", "height: 100", "value: zoo", "id: foobar"]
來自@Željko-Filipin的答案可能過時了......自2015年8月起,他鏈接到的文檔將attribute_value
列為一種方法,可以提取DOM元素的屬性值。
我有commonwatir-4.0.0
, watir-webdriver-0.6.11
和Ruby 2.2.2 - 上面的方法提取了a
標簽的href
,例如。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.