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,340 points
10,745 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,520 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,110 points
6,770 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,700 points
238 views
+1 vote
2 answers
0 votes
2 answers

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,520 points
4,028 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,976 views
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,890 points
2,917 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
3,349 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,520 points
8,377 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,520 points
437 views