Andy
Andy

Reputation: 21

Extract DOM property value using Selenium

In Firebug and other DevTools you can get the DOM properties and values corresponding to an HTML element.

How can such values be extracted using selenium-java code? I had tried getAttribute(), but it seems to be working only for HTML attributes and not for DOM properties like "value" or "spellcheck" etc.

The reason I went for this approach is that the value associated with the <input> text field (snippet below) is run-time generated and data is bound to it using Knockout. And hence it's not possible to capture them with standard approaches like getText(), getAttribute("value"), getAttribute("text"), getAttribute("innerHTML"), getAttribute("innertext"), etc.

HTML snippet for the HTML element:

<input class="form-control" type="text" style="cursor: text" readonly="readonly" data-bind="textInput: url">

Firebug's DOM panel displaying DOM properties of an <input> element

Upvotes: 2

Views: 12344

Answers (3)

Acidblade
Acidblade

Reputation: 1

In Selenium 4 use getDomAttribute() and getDomProperty().

Upvotes: 0

Aidan_Mc_Donnell
Aidan_Mc_Donnell

Reputation: 140

I know this is an old question but it might give someone else a hand out

Use this in the console

$$("input.form-control").value

if it returns the required you will have to execute the Javascript using WebDriver i.e.

driver.ExecuteScript("var data = arguments[0].value; return data;", (Element as RemoteWebElement)

Upvotes: 2

Sebastian Zartner
Sebastian Zartner

Reputation: 20095

According to the Selenium documentation, there is only the getAttribute() function, which is described as follows:

Get the value of a the given attribute of the element. Will return the current value, even if this has been modified after the page has been loaded. More exactly, this method will return the value of the given attribute, unless that attribute is not present, in which case the value of the property with the same name is returned (for example for the "value" property of a textarea element). If neither value is set, null is returned. ...

According to this, getAttribute("value") should return the DOM property value in case there is no HTML attribute named value.

If that's not the case, it may be a timing issue. I.e. the value is read by Selenium before it gets set.

Upvotes: 1

Related Questions