I'm trying to get the contents of a textarea in an HTML form using webdriver in Python.
I'm getting the text, but newlines are missing. The selenium docs are pretty much useless; they say:
class selenium.webdriver.remote.webelement.WebElement(parent, id_)
[...]
text: Gets the text of the element.
I'm currently doing the following:
from selenium import webdriver
# open the browser and web site
b = webdriver.Firefox()
b.get('http://www.example.com')
# get the textarea element
textbox = b.find_element_by_name('textbox')
# print the contents of the textarea
print(repr(textbox.text))
This prints the representation of a Python unicode string of the textarea's contents, except all the newlines have been replaced by spaces. Doh!
Not sure if I'm facing a text encoding issue, selenium/webdriver bug (couldn't find it in the tracker), or user error.
Is there a different way to do this?
EDIT: I just gave Chrome a try... works fine. I reported a bug to selenium's issue tracker. Sam's workaround (the accepted answer below) works in Firefox with one caveat: symbols are converted to HTML entity codes in the returned string. This is no big deal.
I have just got attribute value of tag textarea. Below is a sample of Java code.
WebElement textarea = driver.findElement(By.id("xf-1242"));
String text = textarea.getAttribute("value");
log.debut(text);
I am using Chrome driver, and above code put a text (XML in my case) with newlines in the log. I got the idea from http://www.w3schools.com/jsref/dom_obj_textarea.asp
Jan
As a workaround you can try using ExecuteScript to get the innerHtml. I am not a python guy, but here it is in C#:
IWebElement element = ...
String returnText = ((IJavaScriptExecutor)webDriver).ExecuteScript("return arguments[0].innerHTML", element).ToString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With