@@ -48,10 +48,16 @@ class WebElement(object):
4848 ``StaleElementReferenceException`` is thrown, and all future calls to this
4949 instance will fail."""
5050
51- def __init__ (self , parent , id_ , w3c = False ):
51+ boolean_attributes = ['default' , 'typemustmatch' , 'checked' , 'defer' , 'async' , 'muted' ,
52+ 'reversed' , 'required' , 'controls' , 'ismap' , 'disabled' , 'novalidate' ,
53+ 'readonly' , 'allowfullscreen' , 'selected' , 'formnovalidate' ,
54+ 'multiple' , 'autofocus' , 'open' , 'loop' , 'autoplay' ]
55+
56+ def __init__ (self , parent , id_ , capabilities ):
5257 self ._parent = parent
5358 self ._id = id_
54- self ._w3c = w3c
59+ self .capabilities = capabilities
60+ self ._w3c = "specificationLevel" in self .capabilities
5561
5662 def __repr__ (self ):
5763 return '<{0.__module__}.{0.__name__} (session="{1}", element="{2}")>' .format (
@@ -86,6 +92,24 @@ def clear(self):
8692 """Clears the text if it's a text entry element."""
8793 self ._execute (Command .CLEAR_ELEMENT )
8894
95+ def get_property (self , name ):
96+ """
97+ Gets the given property of the element.
98+
99+ :Args:
100+ - name - Name of the property to retrieve.
101+
102+ Example::
103+
104+ # Check if the "active" CSS class is applied to an element.
105+ text_length = target_element.get_property("text_length")
106+ """
107+ try :
108+ return self ._execute (Command .GET_ELEMENT_PROPERTY , {"name" : name })["value" ]
109+ except WebDriverException :
110+ # if we hit an end point that doesnt understand getElementProperty lets fake it
111+ self .parent .execute_script ('return arguments[0][arguments[1]]' , self , name )
112+
89113 def get_attribute (self , name ):
90114 """Gets the given attribute or property of the element.
91115
@@ -108,12 +132,33 @@ def get_attribute(self, name):
108132 is_active = "active" in target_element.get_attribute("class")
109133
110134 """
111- resp = self . _execute ( Command . GET_ELEMENT_ATTRIBUTE , { 'name' : name })
135+
112136 attributeValue = ''
113- if resp ['value' ] is None :
114- attributeValue = None
137+ if self ._w3c :
138+ if name == 'style' :
139+ return self .parent .execute_script ("return arguments[0].style.cssText" , self )
140+ attributeValue = self .get_property (name )
141+ if (attributeValue in [None , '' , False ] and name != 'value' ) or name in self .boolean_attributes :
142+ # We need to check the attribute before we really set it to None
143+ resp = self ._execute (Command .GET_ELEMENT_ATTRIBUTE , {'name' : name })
144+ attributeValue = resp .get ('value' )
145+
146+ # Even though we have a value, we could be getting the browser default,
147+ # We now need check it's there in the DOM...
148+ resp = self .parent .execute_script ("return arguments[0].hasAttribute(arguments[1])" ,
149+ self , name )
150+ if resp is False :
151+ attributeValue = None
152+ else :
153+ attributeValue = "{0}" .format (attributeValue )
154+
155+ if attributeValue is not None :
156+ if name != 'value' and attributeValue .lower () in ('true' , 'false' ):
157+ attributeValue = attributeValue .lower ()
158+
115159 else :
116- attributeValue = resp ['value' ]
160+ resp = self ._execute (Command .GET_ELEMENT_ATTRIBUTE , {'name' : name })
161+ attributeValue = resp .get ('value' )
117162 if name != 'value' and attributeValue .lower () in ('true' , 'false' ):
118163 attributeValue = attributeValue .lower ()
119164 return attributeValue
0 commit comments