drkthng
drkthng

Reputation: 6929

Why is getText() in Selenium not working for <textarea> elements, but getAttribute("value") is?

Given you have some HTML with a textarea element and want to get its text via Selenium (here Java-binding).

<textarea name="txtComment" id="txtComment" rows="2" cols="20">
    Some comment inside the textarea
</textarea>

This is how I see the code via developer tools (Internet Explorer and Firefox), so it seems like it is a normal text-node and not inside the "value" attribute of the element.

Why is it then that getText() does not work:

driver.findElement(By.id("txtComment")).getText();

It only returns an empty String.

But using getAttribute("value") works and results in returning the expected string:

driver.findElement(By.id("txtComment")).getAttribute("value");

This returns "Some comment inside the textarea" as expected

This is rather surprising since the Selenium documentation about getText() says the following:

Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.

Returns: The innerText of this element.

As the HTML code in the beginning shows, the text-part of the element is visible and it is the "innerText", isn't it?

Can someone shed light on where I'm wrong or if I'm not, why is this implemented like this in Selenium?

Upvotes: 5

Views: 7697

Answers (2)

ashish agarwal
ashish agarwal

Reputation: 11

With Webdriverio (WDIO) tried the same thing.

getText() not worked on Textarea. Returned blank String.

Used getValue() instead, worked for me. Returned the exact string.

<textarea id="txtAreaDescription" maxlength="2500" class="css-pqtyuc">Auto_Internal_Note_Description</textarea>

Upvotes: 1

Mahsum Akbas
Mahsum Akbas

Reputation: 1583

In Python, it gives the same result:

driver.find_element_by_id("txtComment").text
driver.find_element_by_id("txtComment").get_attribute("value")

Some comment inside the textarea.

Upvotes: 2

Related Questions