How to Verify Tooltip using Selenium WebDriver?

We can verify the tooltip of an element using Selenium webdriver using the getAttribute method. A tooltip text is the one which gets displayed while we hover on that element.

It disappears once we move the mouse away from the element. A tooltip text generally displays the title attribute value of an element. First, we identify the element then apply the getAttribute method on it. The parameter to be passed to this method is title.

Let us investigate the html code of an element - Tools having a tooltip text.

Here, the tooltip text displayed from Tools menu is Tools - Online Development and Testing Tools which is the value set for the title attribute in the html.


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 TooltipVerfy{
   public static void main(String[] args) {
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      //identify element
      WebElement n = driver.findElement(By.linkText("Tools"));

      //obtain title attribute
      String s = n.getAttribute("title");
      //verify tooltip text

      if(s.equals("Tools - Online Development and Testing Tools")) {
         System.out.println("Tooltip text matched");
         System.out.println("Tooltip text not matched");