How to verify Tool tips using Selenium WebDriver?

Posted by

Tool tips are used to make a user interface easy to use. These tool tips must be checked for correctness and accuracy. So, how do we verify tool tips using Selenium webdriver?

You may also like to read complete Selenium tutorials for beginners on this blog.

What is a tool tip?

In Google search page, if the mouse moves to the search text box, small “hover box” with text gets displayed. 

This is called a tooltip of web element

verify Tooltips using Selenium WebDriver

Tooltips are hints to the user what type of object it is which gets displayed when the mouse is hovered over some link, text or sometimes images.

As mentioned above, the purpose is to provide a hint to the user what type of object it is.

It is required to verify text displayed is as per the expected text.

For this reason, it is required to retrieve the text inside the tooltip and verify the text. 

This can be done in different ways depending upon how tooltip is getting inserted in the HTML. 

Below listed are 2 cases-

  • Case One: When the tooltip is available in the ‘title’ attribute. Here, we can retrieve tooltip by using getAttribute() method.
  • Case Two: When the tooltip is available in ‘div’ tag. Here, we can retrieve tooltip using Actions class methods.

Let’s start by discussing Case 1…

Case 1: Capturing ToolTip using getAttribute()

ToolTip gets displayed when hovered over an HTML object. 

This can also be seen from the Developer tool of the browser where tooltip text is seen as a value set in ‘title’ attribute. 

Sample Code:

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;



import org.testng.Assert;

public class ToolTip {

static WebDriver driver;

public static void main(String args[])



                    driver = new ChromeDriver();


                    String Exepctedresult= “Search”;

                   WebElement search = driver.findElement(By.xpath(“//input[@class=’gLFyf gsfi’]”));

                   String actualResult =search.getAttribute(“title”);

                   Assert.assertEquals(actualResult, Exepctedresult, “Testcase pass”);

                    System.out.println(“Testcase pass”);



Case 2: Capture tool tip in Selenium using Actions Class

Hover over the object and try to read the description of the tooltip. And this can only be done with the help of Actions Class in Selenium
Let’s see how to use Actions class methods to get it done.

Step 1:

Instantiate an Actions class to make use of its object. 

Actions action = new Actions(driver);

Step 2:

Locate the tooltip web-element. 

WebElement element = driver.findElement(Any By strategy & locator); 

Step 3:

Invoke moveToElement(), this method of Actions class moves the mouse to the middle of the element.


perform() method  is necessary to use, after using moveToElement() method to perform the action.

Example program

import org.openqa.selenium.By;                                                                              import org.openqa.selenium.WebDriver;                                                                   import org.openqa.selenium.WebElement;                                                               import;                                                  import org.openqa.selenium.interactions.Actions;                                         

public class Tooltips2


//Variable declared

  static WebDriver driver;

public static void main(String[] args)


// TODO Auto-generated method stub System.setProperty(“”,”G:\\Maven_Selenium\\SeleniumGroup\\Jar\\chromedriver.exe”);                                                                              

driver = new ChromeDriver();                                                     

//lauching website




//Creating object for Action class

Actions actions = new Actions(driver);

WebElement element = driver.findElement(“tooltipDemo”)); actions.moveToElement(element).perform();

WebElement toolTip = driver.findElement(By.cssSelector(“.tooltiptext”)); 

// To get the tool tip text and assert                                                             

String toolTipText = toolTip.getText()                    


//Verification if tooltip text is matching expected value  if(toolTipText.equalsIgnoreCase(“We ask for your age only for statistical purposes.”))     


System.out.println(“Pass* : Tooltip matching expected value”);          




System.out.println(“Fail : Tooltip NOT matching expectedvalue”);   }

  // Close the main window





In this post, we discussed about how to verify tool tips using Selenium. We have verified the tool tips with the desired text.

Selenium Training with Job readiness package

About Techcanvass

Techcanvass offers IT certification courses for professionals. We are an IIBA endorsed education provider (EEP), iSQI ATP (for Certified Agile Business Analyst Training) as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

We have a Business analyst training course with domain training in-built into it. This training program offers you the opportunity to get certified with ECBA certification as well as have banking domain understanding.

Selenium Certification Training