Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GetAttribute("disabled") return "true" not "disabled"?

In some of my tests I have to confirm that some select2 dropdowns are disabled when certain flags are set. To confirm this I found that the strategy below seemed to work:

Assert.True(element.GetAttribute("disabled").Equals("true"));

When I inspect the element I see disabled="disabled". My question is why does the string returned from GetAttribute = "true" not "disabled"?

like image 844
CynicalBiker Avatar asked Sep 23 '14 12:09

CynicalBiker


People also ask

What does getAttribute return?

The getAttribute() method of the Element interface returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null or "" (the empty string); see Non-existing attributes for details.

What is the return type of getAttribute () and getText () in selenium?

It returns the value of the HTML element's attribute as a string. If a value is not set for an attribute, it will return a NULL value. For attributes with Boolean values, getAttribute() will return either "True" or NULL. The attribute is passed as a parameter to the method.

How do you check if an element is disabled in selenium?

isEnabled() To verify if an element is enabled or disabled on web-page. Returns "ture" if element is enabled and returns "false" if an element is disabled. Examples: Mostly used with button elements, locked/disabled text input elements.

What is getAttribute value in selenium?

As the name suggests, the getAttribute method gets the value of a given HTML attribute. On the other hand, getText() returns the inner text of a given element. The term “inner text” means the text located between the opening and closing tags of the element.


1 Answers

https://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html

Certain attributes seem to only return Boolean values, whether the actual value is "true" or "false". Disabled is one of them. Linked page lists all the attributes considered to be Boolean values, any other attribute should return the attribute's value.

Quoting the most relevant part of the linked page :

"The following are deemed to be "boolean" attributes, and will return either "true" or null: 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 ". [Selenium : WebElement.getAttribute()]

like image 117
RianBattle Avatar answered Dec 04 '22 19:12

RianBattle