Java Selenium WebDriver 中的黑白 getText() 和 getAttribute() 区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32307702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:19:39  来源:igfitidea点击:

Difference b/w getText() and getAttribute() in Selenium WebDriver?

javaseleniumselenium-webdriver

提问by Senthilvel

Both are used to get the WebElement value in between tags? Kindly update whether my assumption is right. If wrong please elaborate it.

两者都用于获取标签之间的 WebElement 值?请更新我的假设是否正确。如有错请详述。

采纳答案by J.Lyu

  <input attr1='a' attr2='b' attr3='c'>foo</input>

getAttribute(attr1)you get 'a'

getAttribute(attr1)你得到'a'

getAttribute(attr2)you get 'b'

getAttribute(attr2)你得到'b'

getAttribute(attr3)you get 'c'

getAttribute(attr3)你得到'c'

getText()with no parameter you can only get 'foo'

getText()没有参数你只能得到 'foo'

回答by Fran Montero

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

getAttribute(String attrName): Get the value of a the given attribute of the element. Will return the current value, even if this has been modified after the page has been loaded. More exactly, this method will return the value of the given attribute, unless that attribute is not present, in which case the value of the property with the same name is returned (for example for the "value" property of a textarea element). If neither value is set, null is returned. The "style" attribute is converted as best can be to a text representation with a trailing semi-colon. The following are deemed to be "boolean" attributes, and will return either "true" or null: async, autofocus, autoplay, checked, compact, complete, controls, declare, defaultchecked, defaultselected, defer, disabled, draggable, ended, formnovalidate, hidden, indeterminate, iscontenteditable, ismap, itemscope, loop, multiple, muted, nohref, noresize, noshade, novalidate, nowrap, open, paused, pubdate, readonly, required, reversed, scoped, seamless, seeking, selected, spellcheck, truespeed, willvalidate Finally, the following commonly mis-capitalized attribute/property names are evaluated as expected: "class" "readonly"

getText():获取该元素的可见(即不被CSS隐藏)innerText,包括子元素,没有任何前导或尾随空格。

getAttribute(String attrName):获取元素的给定属性的值。将返回当前值,即使在页面加载后已修改。更确切地说,此方法将返回给定属性的值,除非该属性不存在,在这种情况下,将返回具有相同名称的属性的值(例如,对于 textarea 元素的“value”属性)。如果没有设置任何值,则返回 null。“样式”属性被尽可能地转换为带有尾随分号的文本表示。以下被视为“布尔”属性,将返回“true”或 null:async、autofocus、autoplay、checked、compact、complete、controls、declare、defaultchecked、defaultselected、defer、disabled、draggable、

getText()return the visible text of the element.

getText()返回元素的可见文本。

getAttribute(String attrName)returns the value of the attribute passed as parameter.

getAttribute(String attrName)返回作为参数传递的属性的值。

回答by Shubham Jain

getAttribute() -> It fetch the text that containing one of any attribute in the HTML tag . Suppose there is HTML tag like

getAttribute() -> 它获取包含 HTML 标签中任何属性之一的文本。假设有像这样的 HTML 标签

<input name="Name Locator" value="selenium">Hello</input>

now getAttribute() fetch the data of the attribute of value which is "Selenium"

现在 getAttribute() 获取值为“Selenium”的属性的数据

Returns: The attribute's current value or null if the value is not set.

返回: 属性的当前值,如果未设置该值,则返回 null。

driver.findElement(By.name("Name Locator")).getAttribute("value")  // The field value is retrieved by the getAttribute("value") Selenium WebDriver predefined method and assigned to the String object.

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

getText() -> 提供 WebElement 的 innerText。获取此元素的可见(即不被 CSS 隐藏)innerText,包括子元素,没有任何前导或尾随空格。

Returns: The innerText of this element.

返回: 此元素的innerText。

driver.findElement(By.name("Name Locator")).getText();

Hello will appear

你好会出现

回答by arunkumar sambu

<img src="w3schools.jpg" alt="W3Schools.com" width="104" height="142">

In above html tag we have different attributes like src, alt, widthand height.

在上面的html标签,我们有像不同的属性srcaltwidthheight

If you want to get the any attribute value from above html tag you have to pass attribute value in getAttribute()method

如果你想从上面的 html 标签中获取任何属性值,你必须在getAttribute()方法中传递属性值

Syntax:

句法:

getAttribute(attributeValue)
getAttribute(src) you get w3schools.jpg
getAttribute(height) you get 142
getAttribute(width) you get 104