Ways to extract text content in Selenium WebDriver

Using getText() method :

All these methods we discuss today, will return the String value of an element. getText() returns visible inner text of an element. Inner text is any text between the opening and closing tags of an element on the DOM. Let's take an example of a Username textbox, below is the DOM structure:

//My By locator will be :
By userNameLabel = By.xpath("(//div/label)[1]");

Using getAttribute() method :

The getAttribute(String attributeName) method takes an attribute String value as parameter. In the above DOM structure, for 'label' tag, we have attributes like 'for'. For 'input' tag, we have attributes like 'type', 'name' and 'id'.

So, if we wanted the attribute value of the 'name' from the 'input' tag, we can write :

driver.findElement(By.id("username")).getAttribute("name"); 
//It will return:  username
driver.findElement(By.id("username")).getAttribute("type"); 
//It will return:  text

Now, let's see the the different attributes using which we can extract the text contents from a DOM : innerText, innerHTML and textContent

System.out.println("Using getText() method :: " 
+ driver.findElement(userNameLabel).getText());
System.out.println("Using getAttribute(innerText) method :: " 
+ driver.findElement(userNameLabel).getAttribute("innerText"));
System.out.println("Using getAttribute(innerHTML) method :: " 
+ driver.findElement(userNameLabel).getAttribute("innerHTML"));
System.out.println("Using getAttribute(textContent) method :: " 
+ driver.findElement(userNameLabel).getAttribute("textContent"));

The output will be :

Using getText() method :: Username
Using getAttribute(innerText) method :: Username
Using getAttribute(innerHTML) method :: Username
Using getAttribute(textContent) method :: Username

They all return the same value i.e. 'Username'. Now let's change our xpath little bit and see the results:

//My By locator will be :
By userNameLabel = By.xpath("//div[@id='form']/div)[1]");
System.out.println("Using getText() method :: " 
+ driver.findElement(userNameLabelText).getText());
System.out.println("Using getAttribute(innerText) method :: " 
+ driver.findElement(userNameLabelText).getAttribute("innerText"));
System.out.println("Using getAttribute(innerHTML) method :: " 
+ driver.findElement(userNameLabelText).getAttribute("innerHTML"));
System.out.println("Using getAttribute(textContent) method :: " 
+ driver.findElement(userNameLabelText).getAttribute("textContent"));

The output will be :

Using getText() method :: Username
Using getAttribute(innerText) method :: Username 
Using getAttribute(innerHTML) method :: 
            <label for="username">Username</label>
            <input type="text" name="username" id="username">

Using getAttribute(textContent) method :: 
            Username

The 'innerHTML' attribute returns the complete inner HTML of the 'div' parent element.

Note : The getText() and getAttribute("innerText") might not return the same value in every sceanrio. It depends on the DOM structure, browser type etc.