问题描述
鉴于您有一些带有textarea元素的HTML,并且希望通过Selenium(此处为Java绑定)获取其文本.
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>
这就是我通过开发人员工具(Internet Explorer和Firefox)查看代码的方式,因此看来这是一个普通的文本节点,而不是在值"内部.元素的属性.
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.
为什么 getText()不起作用:
driver.findElement(By.id("txtComment")).getText();
仅返回空字符串.
但是使用 getAttribute("value")可以起作用,并导致返回预期的字符串:
But using getAttribute("value") works and results in returning the expected string:
driver.findElement(By.id("txtComment")).getAttribute("value");
此返回"textarea内的某些注释".符合预期
这令人惊讶,因为有关 getText()表示以下内容:
This is rather surprising since the Selenium documentation about getText() says the following:
返回:该元素的innerText.
Returns:The innerText of this element.
如开头的HTML代码所示,元素的文本部分可见,它是"innerText",不是吗?
As the HTML code in the beginning shows, the text-part of the element is visible and it is the "innerText", isn't it?
推荐答案
在Python中,结果相同:
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.
这篇关于Selenium中的getText()为什么不能用于< textarea> ;?元素,但getAttribute("value")是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!