17

Both are used to get the WebElement value in between tags.

Is my assumption right? If wrong, please elaborate.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Senthilvel
  • 281
  • 1
  • 3
  • 10
  • In the below case by using `getText()` its possible to get webElement value (i.e) Manual Tester , Automation Tester (E.g:) ` Manual Tester ` – Senthilvel Aug 31 '15 at 09:33
  • If you leave a downvote, please explain to me, why you downvoted, so I can improve my answer/take your point into consideration for future answers. – Senthilvel Aug 31 '15 at 10:43
  • You could google the two and read the docs that explain what the difference is. That's probably why the question was downvoted. – JeffC Aug 31 '15 at 15:51
  • 2
    @JeffC To get an answers in an easy and understandable way I have raised questions here. But If its downvoted like this, then its no use of raising questions. – Senthilvel Sep 02 '15 at 07:31
  • 4
    The point is that you need to do some basic research and have at least a basic understanding of what you are asking *before* you ask the question. You clearly didn't do that because if you just google your own question you would have found the answer. We are not your research team. We don't get paid to answer questions. The expectation is that you try to solve your own problems. Once you've put in a reasonable effort and are still stuck, you *then* ask questions stating your question, what you have tried, and what errors you are getting. [Read this](http://stackoverflow.com/help/how-to-ask) – JeffC Sep 02 '15 at 13:54

4 Answers4

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

getAttribute(attr1) you get 'a'

getAttribute(attr2) you get 'b'

getAttribute(attr3) you get 'c'

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

iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
J.Lyu
  • 932
  • 7
  • 16
  • But does not work [for a text box in an HTML form](https://stackoverflow.com/questions/22087952/how-to-gettext-of-an-element-in-selenium-webdriver)? – Peter Mortensen Nov 22 '20 at 08:02
10

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

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

Now getAttribute() fetches the data of the attribute of 'value', which is "Selenium".

Returns:

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

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.

Returns:

The innerText of this element.

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

'Hello' will appear

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
3
<img src="w3schools.jpg" alt="W3Schools.com" width="104" height="142">

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

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

Syntax:

getAttribute(attributeValue)
getAttribute(src) you get w3schools.jpg
getAttribute(height) you get 142
getAttribute(width) you get 104 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
arunkumar sambu
  • 653
  • 1
  • 9
  • 22
0

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() return the visible text of the element.

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

Fran Montero
  • 1,679
  • 12
  • 24