How can you fetch an attribute from an element? How to retrieve typed text from a textbox in Selenium?

We can fetch the attribute of an element by using the getAttribute() method.

Sample code:

WebElement eLogin = driver.findElement("Login");
String LoginClassName = eLogin.getAttribute("class");

Here, we are finding the web page's login button named "Login". Once that element is found, getAttribute() can be used to retrieve any attribute value of that element and it can be stored it in string format. In my example, We have retrieved "class" attribute and stored it in LoginClassName.

Similarly, to retrieve some text from any textbox, we can use getText() method. In the below piece of code I have retrieved the text typed in the "Login" element.

WebElement eLogin = driver.findElement("Login");
String LoginText = Login.getText ();