How do I get the text contents of a textarea with

2020-07-10 06:18发布

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.

3条回答
虎瘦雄心在
2楼-- · 2020-07-10 06:59

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();
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-07-10 07:06

In Python get the element first, and after get the attribute value, function in python get_attribute('value').

from selenium import webdriver

driver = webdriver.Firefox()
URL = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_textarea"
driver.get(URL)
driver.switch_to.frame("iframeResult")
# get the textarea element by tag name
textarea = driver.find_element_by_tag_name('textarea')

# print the attribute of the textarea
print(textarea.get_attribute('value'))
print(textarea.get_attribute('rows'))
print(textarea.get_attribute('cols'))
查看更多
来,给爷笑一个
4楼-- · 2020-07-10 07:12

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

查看更多
登录 后发表回答