Differences between getText() & getAttribute() methods?

What are differences between getText() and getAttribute() methods?

WHAT IS GETTEXT() METHOD? 

  • getText() is a method which gets you the visible  inner Text of this element, including sub-elements, without any leading or trailing white space.
  • Inner text is text between the opening tags and closing tags.
    For example: <a> I am Automation Engineer</a>

In above example, texts “I am Automation Engineer” between opening and closing tags are called inner text of web element “a”.

  • We can retrieve innerText of element using getText() method.
  • Return type of getText() method is String.

WHY WE USE GETTEXT() METHOD?

  • Maximum links, labels etc in a web page are inner text which are displayed on webpage for end users. So we need to verify weather correct text are displayed or not.
  • It is also useful to locate proper elements while writing automation script based on inner text value.

HOW TO USE GETTEXT() METHOD?

  • getText() method is used on an element as innerText belongs to an web element.

Let’s see an Example below:

Here we retrieve text “Sariful Islam” from Codenbox AutomationLab site. Inner text “Sariful Islam” is in between td tag. So if we locate td web element and use getText() method, it will return the text between <td>tags as below: 

System.out.println(driver.findElement(By.xpath(“//*[@id=”product”]/tbody/tr[2]/td[1]”)).getText());

Output:   Sariful Islam

  • If you do not have any inner text between tags, getText() method will return null.

Example: <someTag></someTag>


WHAT IS GETATTRIBUTE() METHOD?

  • getAttribute() is method which is declared in WebElement interface.
  • It returns the value of the given attribute as a String of the Web element.
  • More exactly, this method will return the value of the property with the given name, if it exists. If it does not, then the value of the attribute with the given name is returned. If neither exists, null is returned.
  • getAttribute() method will return either “true” or null for attributes whose value is Boolean.
  • The following commonly mis-capitalized attribute/property names are evaluated as expected:
    • If the given name is “class”, the “className” property is returned.

HOW TO USE GETATTRIBUTE() METHOD?

getAttribute() method work on specific web element. So first you need to locate a web element and then you need to call getAttribute() method on it by specifying attribute for which value you require.

Let’s see an example: The below Html code for radio button.

<input value=”radio1″ name=”radioButton” class=”radioButton” type=”radio” xpath=”1″>

The above web element (radio button) has many attribute as value, class, type and so on. But there is no closing </input> tag. So in this element we need to use getAttribute(String attributName) method as below:

driver.findElement(by.xpath(“//*[@value=’radio1′]”)).getAttribute(“value”);

OutPut: radio1


Differences between this two methods are:

  • getAttribute(String attributName) is used to retrieve given attribute value.
  • getText() is used to retrieve inner text on web element.

Share the Knowledge

You May Also Like

About the Author: codenbox

Leave a Reply

Your email address will not be published. Required fields are marked *