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,220 points
4,263 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,050 points

Related Questions In Selenium

+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
• 56,160 points
72 views
0 votes
1 answer

What is the difference between findElement and findElements in Selenium Webdriver?

Hi Nilaabh, Selenium Webdriver uses findElement and ...READ MORE

answered May 8 in Selenium by Anvi
• 9,910 points
107 views
0 votes
1 answer

What is Xpath and what are the types of it in Selenium Webdriver?

Hi Piyush, XPath is defined as XML path. It is ...READ MORE

answered May 8 in Selenium by Pratibha
• 2,740 points
79 views
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,050 points
248 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,130 points
621 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,130 points
243 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,050 points
2,373 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,050 points
155 views