How to get a list of element attributes using watir webdriver

I am trying to write a webdriver watir script that retrieves the attributes of an element and then gets their values. given element

<input id="foobar" width="200" height="100" value="zoo" type="text"/> 

hope i can do something like the following:

  testElement = $b.element(:id, "foobar") testElement.attributes.each do |attribute| puts("#{attribute}: #{testElement.attribute_value(attribute)}") end 

I would like to get

  id: foobar width: 200 height: 100 value: zoo type: text 
+4
source share
3 answers

I have seen people using javascript to get a list of attributes. The following shows how you can add a method to Watir :: Element to get a list of attributes (although the Watir extension is optional).

 #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"] 
+4
source

The answer from Željko-Filipin can be dated ... by the document it refers to, starting in August '15, lists attribute_value as a method that retrieves, well, the attribute value for the DOM element.

I have commonwatir-4.0.0 , watir-webdriver-0.6.11 and Ruby 2.2.2 - the method described above highlights the href tag, for example.

+2
source

What are you trying to do?

As far as I can see, the Element class does not have the #attributes method: http://watir.github.com/watir-webdriver/doc/Watir/Element.html

You can get the HTML element and parse it:

 browser.element.html 
+1
source

Source: https://habr.com/ru/post/1434952/


All Articles