How to Get Inner Text in Selenium Java

Selenium is an open-source web-based automation tool that is implemented using a web driver. We will be using geckodriver because Selenium 3 enables geckodriver as the default WebDriver implementation for Firefox.


  1. geckodriver.exe
  2. maven dependency selenium


Getting inner text takes 4 simple steps:
  1. Set webdriver.gecko.driver and its' path as a system property.
  2. Set the firefox diver and browse to the website.
  3. Get the HTML Div containing text as Web-element using webdriver.findElement() function and css selector.
  4. Find a text element and print its text.

Let’s see all the above steps in the code. We will use Webelment.getText() function to get the inner text of the element.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GetInnerText {
    public static String GECKODRIVER_PATH ="F:\\WORK\\SeleniumShortTasks\\InnerText\\src\\main\\resources\\geckodriver.exe";
    public static void main(String[] args) {
        //set firefox webdriver
        System.setProperty("webdriver.gecko.driver", GECKODRIVER_PATH);
        WebDriver driver = new FirefoxDriver();

        //get the firefox browser & Browse the Website for text
        String siteLink = "";

        //Get Heading Div
        WebElement headingDiv = driver.findElement(By.cssSelector("div.kno-rdesc"));
        //Get inner Element for our text
        WebElement span = headingDiv.findElement(By.tagName("span"));
        System.out.println("Heading inner Text = "+span.getText());


All resources used in this tutorial are attached:
  • source code
  • geckdriver.exe