Selenium中的getText()为什么不能用于< textarea&gt ;?元素,但getAttribute("value")是? [英] Why is getText() in Selenium not working for <textarea> elements, but getAttribute("value") is?

查看:48
本文介绍了Selenium中的getText()为什么不能用于< textarea&gt ;?元素,但getAttribute("value")是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于您有一些带有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内的某些注释".符合预期

这令人惊讶,因为有关

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

获取此元素的可见(即未被CSS隐藏)的innerText,包括子元素,没有任何前导或尾随空格.

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

返回:该元素的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()为什么不能用于&lt; textarea&gt ;?元素,但getAttribute("value")是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆