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,240 points
6,272 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,430 points

Related Questions In Selenium

0 votes
1 answer
+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,300 points
112 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
• 13,960 points
764 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,430 points
1,181 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,170 points
1,267 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,170 points
1,024 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,430 points
3,941 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,430 points
245 views