What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

0 votes
I know that both are used to get the WebElement value in between tags? Can someone please tell me what exactly is the difference, and what extra can getAttribute() retrieve (well, that's my assumption)?
Apr 5, 2018 in Selenium by Martin
• 4,330 points
8,450 views

1 answer to this question.

0 votes

See, both are used to retrieve something from the webelement.
getText() simply retrieves the visible text present in the webelement. So, this is the text inbetween 2 HTML tags (openning & closing tags)
getAttribute() however, retrieves the value of attribute(s) of that webelement. So, this is the key & value pairs inside the tags(mostly openning tags).

Look at the below example to understand this better.

<input attra='1' attrb='2' attrc='3'>Edureka</input>

getAttribute(attra) --> you get value '1' returned
getAttribute(attr2) --> you get value '2' returned
getAttribute(attr3) --> you get value '3' returned
getText() --> with no parameter returns only 'Edureka'.
answered Apr 5, 2018 by nsv999
• 5,510 points

getText() is a method which gets us the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing white space.

The getAttribute() method is declared in the WebElement interface, and it returns the value of the web element’s attribute as a string. It fetches the value of an attribute, in HTML code whatever is present in the left side of '=' is an attribute, value on the right side is an attribute value.

More  details - https://configureselenium.blogspot.com/2020/02/difference-between-gettext-and.html

Related Questions In Selenium

0 votes
1 answer

What are the differences between POM and PageFactory in Selenium Webdriver?

Hi Raveena, Page Object Model (POM) and ...READ MORE

answered Jul 17, 2019 in Selenium by Anvi
• 14,050 points
2,903 views
0 votes
2 answers
+1 vote
1 answer

What are the similarity between Selenium RC and Selenium Webdriver?

Selenium RC and Webdriver both support the ...READ MORE

answered Dec 12, 2018 in Selenium by Priyaj
• 57,530 points
171 views
+1 vote
2 answers
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
2,574 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,870 points
2,037 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,870 points
2,131 views
0 votes
1 answer

What's the difference between functions getClass() and getAttribute(“class”) in Selenium?

The simple difference is that, getClass() returns ...READ MORE

answered Apr 7, 2018 in Selenium by nsv999
• 5,510 points
6,366 views
0 votes
1 answer

What's the diff between Selenium RC and Selenium WebDriver?

RC works by injecting the JavaScript functions ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,510 points
341 views