Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to getText from a disabled input field in Selenium Java

Tags:

java

selenium

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.

like image 205
Prasanna Lakshmi Avatar asked Dec 11 '15 09:12

Prasanna Lakshmi


1 Answers

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()");
like image 163
Pascal Avatar answered Oct 04 '22 04:10

Pascal