How to get Tooltip Text in Selenium Webdriver?

We can get the tooltip text in Selenium webdriver with help of the method - getAttribute. The attribute title should be passed as a parameter to this method.

This technique is only applicable if the element has a title attribute.

The tooltip text is the one which gets displayed on hovering the mouse over the element. In the below html code, an element having a tooltip has the attribute title and the value set for title is actually the tooltip text.

The below image shows the menu Coding Ground showing the tooltip text as - Coding Ground - Free Online IDE and Terminal.


WebElement m = driver.findElement(By.linkText("Coding Ground"));
String s = m.getAttribute("title");


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class ToolTipTxt{
   public static void main(String[] args) {
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      // identify element
      WebElement l=driver.
      findElement(By.linkText("Coding Ground"));
      // get title attribute value
      String t = l.getAttribute("title");
      System.out.println("Retrieved tooltip text as :" +t);


Updated on: 03-Apr-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started