8

How can I get text from a disabled input field in Selenium Java?

Below is the HTML tag.

<input id="endDate" class="ng-pristine ng-untouched ng-valid ng-valid-maxlength" data-ng-disabled="dateRange!=='Cm'" size="10" maxlength="10" data-ng-model="endDate" validate-date="" name="endDate" disabled=""/>

I'm looking for Selenium Java code to get the text value from that disabled input field.

I tried getAttribute("disabled"). But it is returning true. I tried WebElement.getAttribute("id"), but it is returning null value. None of it worked.

The value of that field will be generated dynamically. For example, if I select today the values will be populated as SYSDATE. For the yesterday value will be SYSDATE-1.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prasanna Lakshmi
  • 81
  • 1
  • 1
  • 6
  • 2
    Have a look at this topic, as it looks like the same problem : http://stackoverflow.com/questions/11429070/selenium-wd-get-value-of-disabled-input – Arnaud Dec 11 '15 at 09:29

1 Answers1

11

Try:

webElement.findElement(By.cssSelector("#endDate")).getAttribute("value")

Or:

webElement.findElement(By.cssSelector("#endDate")).getText()

You have to try it out. It depends on your special case. If these variants don't work, check if your selectors are correct.

If all of them didn't work, try to get the value over angular.element as below:

return (String) ((JavascriptExecutor) this.webDriver).executeScript("angular.element($('#endDate')).text()");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Synoon
  • 2,297
  • 4
  • 22
  • 37