getText and getAttribute in Selenium

Get Text vs Get Attribute in Selenium

What’s the Difference?


One of the most common questions in Selenium interviews—is Get Text vs Get Attribute in Selenium. At first glance, both methods appear to retrieve information from a web element. However, they serve completely different purposes. In this guide, you’ll learn exactly when to use each method, complete with practical Java examples and real-world automation scenarios.


What is getText()?

The getText() method returns the visible text displayed between an element’s opening and closing HTML tags.

HTML Example

<button>Login</button>

Selenium Java

WebElement loginButton = driver.findElement(By.tagName("button"));
String text = loginButton.getText();
System.out.println(text);

Output

Login

getText() retrieves only what the user can actually see on the webpage.


What is getAttribute()?

The getAttribute() method returns the value of a specified HTML attribute.

HTML Example

<input
type="text"
id="username"
name="username"
value="John"
placeholder="Enter username">

Selenium Java

WebElement username = driver.findElement(By.id("username"));
System.out.println(username.getAttribute("value"));
System.out.println(username.getAttribute("placeholder"));
System.out.println(username.getAttribute("type"));

Output

John
Enter username
text

Unlike getText(), this method reads information stored inside HTML attributes.


Quick Comparison

FeaturegetText()getAttribute()
Reads visible text✅❌
Reads HTML attributes❌✅
Works well with labels✅❌
Works with input fields❌✅
Reads placeholder❌✅
Reads value attribute❌✅
Used for buttons, links, headings✅Sometimes

Example 1: Reading a Button Label

HTML

<button>Sign In</button>

Selenium

WebElement button = driver.findElement(By.tagName("button"));
System.out.println(button.getText());

Output

Sign In

Using getAttribute("value") here would usually return null because the button text is not stored in an attribute.


Example 2: Reading Text from an Input Box

HTML

<input
type="text"
value="John Doe">

Incorrect Approach

WebElement textbox = driver.findElement(By.xpath("//input"));
System.out.println(textbox.getText());

The output is empty because an <input> element doesn’t contain visible inner text.

Correct Approach

System.out.println(textbox.getAttribute("value"));

Output

John Doe

This is one of the most common Selenium interview questions.


Common Attributes Used with getAttribute()

Some frequently used attributes include:

  • value
  • id
  • name
  • class
  • href
  • src
  • placeholder
  • title
  • alt
  • type
  • disabled
  • checked
  • readonly

Real-World Automation Scenarios

Validate a Welcome Message

String message = driver.findElement(By.id("welcome")).getText();
Assert.assertEquals(message, "Welcome John");

Use getText() because you’re validating visible content.


Validate a Pre-filled Username

String username = driver.findElement(By.id("username")).getAttribute("value");
Assert.assertEquals(username, "john@test.com");

Use getAttribute("value") because the data is stored in the input field’s value attribute.


Verify Image Source

String image = driver.findElement(By.id("logo")).getAttribute("src");

Perfect for validating logos and other images.


Verify Link Destination

String href = driver.findElement(By.linkText("Products")).getAttribute("href");

Useful for checking navigation targets without clicking the link.


Common Mistakes

Using getText() on an Input Field

❌

textbox.getText();

✔ Use:

textbox.getAttribute("value");

Using getAttribute("value") on a Button

<button>Submit</button>

Most buttons don’t have a value attribute.

Use:

button.getText();

Best Practices

  • Use getText() for visible labels, messages, headings, buttons, and links.
  • Use getAttribute("value") for text entered in input fields.
  • Use getAttribute("href") to verify links.
  • Use getAttribute("src") to validate images.
  • Use getAttribute("placeholder") to verify placeholder text.
  • Avoid using getText() on <input> elements, as it generally returns an empty string.

Final Thoughts

Although getText() and getAttribute() are both used to retrieve information from web elements, they solve different problems.

Remember this simple rule:

  • Use getText() when you need what the user can see.
  • Use getAttribute() when you need information stored in the HTML attributes.

Understanding this distinction will help you write more reliable Selenium tests, avoid common automation mistakes, and confidently answer interview questions.


Related Links


Discover more from Rotebit

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply