How to get text of a disabled or read only element developed by Angular using Selenium C#

2,341 views
Skip to first unread message

Shruthi Donthiri

unread,
May 25, 2020, 3:54:34 AM5/25/20
to Selenium Users
We have a application developed with Angular. And there is a input field which it is read only/disabled. How to get the text of the field. 
I have tried to get the text using Text and GetAttribute("value"). But unable to get the text. 

Can anyone suggest here?

Joe Ward

unread,
May 25, 2020, 4:45:40 AM5/25/20
to seleniu...@googlegroups.com
What does "unable to get the text" mean? How were you unable to get it? getAttribute("value") is indeed how you do this so I don't know what the issue would be. Can you share how you're using it/the page you are using it against?

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/cd35fd6a-1b51-49b6-84db-6994101cb47d%40googlegroups.com.

Shruthi Donthiri

unread,
May 25, 2020, 5:18:29 AM5/25/20
to Selenium Users

I have used below ways to get the value of the input field which is disabled in Page. Value is not returned with below when it is enabled for Edit then i'm able to get the data with the same XPath.


driver.FindElement(By.XPath("//*[@ng-reflect-name='mobileNumber']")).GetAttribute("value").ToString();

driver.FindElement(By.XPath("//*[@ng-reflect-name='mobileNumber']")).Text;


On Monday, 25 May 2020 14:15:40 UTC+5:30, That Guy wrote:
What does "unable to get the text" mean? How were you unable to get it? getAttribute("value") is indeed how you do this so I don't know what the issue would be. Can you share how you're using it/the page you are using it against?

On Mon, 25 May 2020 at 08:54, Shruthi Donthiri <shruthi...@gmail.com> wrote:
We have a application developed with Angular. And there is a input field which it is read only/disabled. How to get the text of the field. 
I have tried to get the text using Text and GetAttribute("value"). But unable to get the text. 

Can anyone suggest here?

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to seleniu...@googlegroups.com.

Adrian

unread,
May 26, 2020, 5:13:41 PM5/26/20
to Selenium Users
Hi Shruthi,
Can you please post the elements of the field.  It helps me to think.

Cheers,
Adrian.

Archana Singh

unread,
May 27, 2020, 12:08:31 AM5/27/20
to Selenium Users
If your input is readOnly thats why you getting error then disable readonly. Use remove attribute.

Archana Singh

unread,
May 27, 2020, 12:12:24 AM5/27/20
to Selenium Users
Eg -

((JavascriptExecutor)driver).executeScript ("document.getElementById('"').removeAttribute('readonly',0);");

Ripon Al Wasim

unread,
May 27, 2020, 3:10:30 AM5/27/20
to seleniu...@googlegroups.com
Hi Shruthi Donthiri,
It could be done by using JS, JavascriptExecutor. First of all it needs to change the value of type attribute as text from hidden.
The following concept could be helpful:

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

Ripon Al Wasim

unread,
May 27, 2020, 3:19:24 AM5/27/20
to seleniu...@googlegroups.com
Hi,
Before getting text apply the following line of code:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('<yourElementID>').setAttribute('type', 'text');");
Then apply getText() command

Shruthi Donthiri

unread,
May 27, 2020, 5:59:23 AM5/27/20
to Selenium Users
Is there any other method instead of getElementById as i cant use the ID as it is changing dynamically.
Tried to use Class but not working.

Archana Singh

unread,
May 27, 2020, 6:12:28 AM5/27/20
to Selenium Users
You can use document.evaluate( xpathExpression, contextNode, namespaceResolver, resultType, result )

Ripon Al Wasim

unread,
May 27, 2020, 6:12:41 AM5/27/20
to seleniu...@googlegroups.com
Hi Shruthi Donthiri,
If the element has name you can use it.

To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/f61b97be-d18f-4612-a986-6dabf2f6fbbf%40googlegroups.com.

Shruthi Donthiri

unread,
May 27, 2020, 6:19:06 AM5/27/20
to Selenium Users
No element does not has name, application is developed with Angular so there are angular tags.

Shruthi Donthiri

unread,
May 27, 2020, 6:41:25 AM5/27/20
to Selenium Users
I'm getting "javascript error: error reading property" error. I have used the following code.
Please advise if anything is missing.

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;

var confirmationtext = "return document.evaluate( \"//*[@ng-reflect-name='mobileNumber']\",  document,  null,  XPathResult.ANY_TYPE,  null)";

var returnvalue = js.ExecuteScript(confirmationtext);

Villju Joseph

unread,
May 27, 2020, 11:10:32 AM5/27/20
to seleniu...@googlegroups.com
Hi,
When it's disabled, what's the corresponding source page code for the element. Can u share the same. 
Hope u have used geyText().
Thanks, 
Villju


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages